DEV Community

loading...

Cron job for node scripts.

prakhil_tp profile image Prakhil Updated on ・4 min read

You can use a cron job for any program which runs on Linux by using the following method. Here I'm saying about making the cron job for node js scripts.

Tl;dr

  • Create a script.sh file to trigger the node js script.
  • Make use of crontab -e command to create the cron job.
  • The job will point to script.sh file.
  • We troubleshoot the job by looking at the logs.

Contents

Cron is a useful tool in Linux that most developers love because it lets you run automated commands at specific periods (minutes, hours, days, etc.).

Cron jobs run in the background and its keep checking few files and directories (/etc/crontab/, /etc/cron.*/ and var/spool/cron ..etc). Cron files are not supported to be edit directly, and each user has a unique crontab. The crontab stands for the cron table. It is the list of commands that you want to run on a regular schedule.

Commands

The commands for creating and editing cron jobs are the same and simple. And what's even cooler is that you don't need to restart the cron after making changes to the existing one.
cool! right?

let's view our cron table entries before creating a new one.

crontab -l 
Enter fullscreen mode Exit fullscreen mode

It will list if it has any. Anyway, let's move on to the step to create a cron job.

crontab -e
Enter fullscreen mode Exit fullscreen mode

Nothing special, single parameter change to command!.
The command may prompt you to select a text editor(nano, vim), go on with the comfortable one.

Now, we have a crontab file opened on a text editor to write our entries.

Cron syntax

Just as it is with any language, working with cron is a lot easier when you understand its syntax.

MIN HOUR DOM MON DOW CMD

Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.

That's not all. Cron uses 3 operator symbols which allow you to specify multiple values in a single field.

  • Asterisk (*) - specifies all possible values for a field.
  • The comma (,) - specifies a list of values.
  • Dash (-) - specifies a range of values.
  • Separator (/) - specifies a step value.

By looking at the syntax we can write our entries.

0 3 * * * /home/user/path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

Meaning, Run /home/user/path/to/script.sh at 3 am every day.
You can adjust periods by changing the time parameters.

*/30 * * * * /home/user/path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

The above one will run the script every 30 minutes.

30 * * * * /home/user/path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

This would run at 1:30,2:30,3:30.

0,30 * * * * /home/user/path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

This would run at 1:30,2:00,2:30,3:00,3:30.

Write this entry on the text editor we're previously opened using
crontab -e command. Before saving it we need to set up our script for this.

Setting up node script

You might notice the script.sh above. Yes, it is a bash script.
you can call your node js script using bash.
For this, create a file touch script.sh anywhere you want,
and follow these steps.

How do you run your node script normally on your
terminal?

  • cd /path/to/directory/
  • node index.js

These bash commands will help us to do it, right?

So, we can write it to our script.sh file with a little change. Cron doesn't support relative path, therefore you've to write the absolute path for everything that you are pointing.

You can find the absolute paths of directories by simply typing pwd on your terminal. The installation path of the node can get by the command which node.

script.sh

cd /home/user/path/to/nodejs/script/directory/
/home/user/.nvm/versions/node/v12.17.0/bin/node /home/user/index.js

Enter fullscreen mode Exit fullscreen mode

Save it and make our script.sh file executable.

chmod +x script.sh
Enter fullscreen mode Exit fullscreen mode

That's it!

Save it all to run the cron job for the specified time period.

Troubleshoot

Cron jobs are commands that your service runs at a specified interval and, as such, can be difficult to troubleshoot.

Although we can't directly troubleshoot, some of the common mistakes are:

  1. Using a relative path. You must be sure to use only absolute paths inside that script.
  2. Permissions are too strict. Please be sure all scripts, files that are being used are set to executable. chmod +x <file name>

You can check the cron logs to make sure that the crontab is working correctly. The logs are default located in
/var/log/syslog. And running the following grep command will get you all cron logs.

grep cron /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

Conclusion

Let's go through the steps we've done.

  • Created a script.sh file to trigger the node js script.
  • Add an entry to the cron table using crontab -e command.
  • The crontab entry should point to the script.sh file.
  • We troubleshoot the job by looking at the logs

Discussion (0)

pic
Editor guide