Reading and writing files with fs in Node during a cron job

Written by Michael Lee on May 12, 2018

Utilizing cron is one of the ways I automate my projects. Cron is a utility that runs jobs based on a specified interval. Cron is what I use to run @tinyracesmy Twitter bot that tweets out emoji races. It’s also how I make database backups to S3 for web applications.

Depending on the project, there are times I like to use reading and writing to the file system for persistence of data for a project. Since I write most of my apps in JavaScript, I use Node’s fs module which gives me the ability to read and write to the file system.

Usually the file I’m reading and writing to is located within the root directory of the project folder. This makes it easy to access since I can use relative paths to access the file. For example if the file that I’m trying to access is called data.txt and it’s located as a sibling to the file I’m accessing the file from, I can access it with ./data.txt.

But when setting up the script to run via cron, I kept running into issues, where the cron would say that it’s running into an EACCES issue. Thinking this was a permissions issue, I tried changing the file owner of the file that my script is trying to access to root. But I was still running into EACCES. Upon reading about who owns a job and what permissions cron jobs with crontab have, I realized it’s the same as the user who created the job. So it wasn’t an issue of permission since I can manually run the script with no issues.

I was curious if the cron job couldn’t understand the location of the file I was trying to access. Since I was running the script relative to the project folder, but perhaps cron wasn’t running it relative to the project folder.

So then, I updated the script to read and write with an absolute path to the file I was trying to access. Since my environments for developing (laptop running macOS) and for running my apps (Digital Ocean droplet running Ubuntu) differ I use this line of code that first determines the environment in which the script is running and then assigns the home directory to a variable.

// Using process.env['HOME'] I'm able to get the home path on Unix-like systems
let home = process.env['HOME']

Once I have my home directory, I tell my script the location of the file it’s trying to access.

So now when I’m using fs.readFile, it looks like this:

fs.readFile(`${home}/path_to_project/data.txt, 'utf8', (err, data) => {

If you’re running Node scripts with cron and utilizing file systems to persist data, make sure to update your script to use the absolute path of the file you’re trying to access instead of the relative. Otherwise you may risk not having your cron jobs running properly.

Thanks for taking the time to read this article. I'd love to stay in touch and share more tips on programming and design, side projects and working remotely. Sign up and I'll send you my articles straight to your email.

:wave: Hey hey, I'm Michael Lee! I really appreciate you taking the time to read this article.

Be sure to check out my other articles.