Run Periodic Tasks in Node with node-cron

What is Cron?

Cron is a scheduling utility that runs as a daemon process in the background of Unix-like systems. It's extremely popular for running periodic tasks, which can be anything you choose, like initiating a backup or clearing data from a database.

You can add tasks to Cron via the command line using the crontab -e command. Using the -e argument will put it in to edit mode and open your default editor where you can add the job. The syntax of a command looks like this:

1 2 3 4 5 [USER] /path/to/command arg1 arg2  

The numbers 1-5 represent different time increments you can specify for scheduling the job. More details below:

  • 1: Minute (0-59)
  • 2: Hour (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12, where 12 is December)
  • 5: Day of Week (0-7, where Sunday is 0 or 7)

The USER argument is optional and is used to specify which user the command should be run as. You can also specify the user using the -u option. There are a lot more options and variations of the scheduling parameters, but we won't go in to any more detail here. There are plenty of other resources on the internet where you can find these details.

Here is a simple example of how to schedule a script to be run by Cron:

45 17 3 * * /path/to/  

Cron would then run this script at 5:45pm on the 3rd day of every month. Although the syntax can be a bit confusing at first, it is actually a very powerful way to schedule repeating tasks. This format has been adopted by many programs and utilities for specifying schedules.dq4


What is it?

The Cron utility has become synonymous with scheduling tasks, which is how node-cron got its name. It doesn't actually use or interface with the Cron utility underneath, but instead it simply schedules and runs periodic tasks for you using Node's setTimeout() function. It does this by parsing the Cron format schedule you specify, figures out when the next 'tick' should be, and sets the timeout accordingly.

How to use it

To use node-cron, you will have to schedule a function to run programmatically, which might actually be more convenient for you than having to mess with the command line. A simple function can be scheduled like this:

var cron = require('cron');

var job = new cron.CronJob('* * * * *', function() {  
    console.log('Function executed!');
}, null, true);

When running this code, you'll see the text 'Function executed!' printed to the console once every minute.

You can even get a bit fancier and specify the timezone in which the task should run. Or, another option, is to tell node-cron to not start the job yet. This way you can initialize the job and then use the .start() and .stop() methods only when you need it. See the full documentation for a full list of parameters and options.

Advantages and Disadvantages

Personally, I really like node-cron because of how easily you can programmatically schedule code to run. And since it runs on Node, you can use it on any system that supports Node (like Windows), and not just Unix-like systems. So whatever you're using it for, the code is more system independent than it would be if it used Cron directly.

The biggest downside, in my opinion, is that since it doesn't use Cron underneath, we don't get the persistence that we would have otherwise with Cron. What I mean by this is on a system restart, our 'job' wouldn't be running anymore. Instead, you'll have to do some extra work to get the Node process running again, like setting it up to run on startup by placing/linking it in /etc/init.d/. It isn't very hard to do, but it also isn't very convenient.