Documentation

The node-cron module is tiny task scheduler in pure JavaScript for node.js based on GNU crontab. This module allows you to schedule task in node.js using full crontab syntax.

Getting Started

Install node-cron using npm: $ npm install --save node-cron

Import node-cron and schedule a task:

    
    
var cron = require('node-cron');

cron.schedule('* * * * *', () => {
    console.log('running a task every minute');
});
            
    

Cron Syntax

This is a quick reference to cron syntax and also shows the options supported by node-cron.

Allowed fields

            
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
            
        

Allowed values

fieldsvalues
second0-59
minute0-59
hour0-23
day of month1-31
month1-12 (or names, e.g: Jan, Feb, March, April)
day of week0-7 (0 or 7 are sunday, or names, e.g. Sunday, Monday, Tue, Wed)

Using multiples values

You may use multiples values separated by comma:
    
    
var cron = require('node-cron');

cron.schedule('1,2,4,5 * * * *', () => {
    console.log('running every minute 1, 2, 4 and 5');
});
        
    

Using ranges

You may also define a range of values:
    
    
var cron = require('node-cron');

cron.schedule('1-5 * * * *', () => {
  console.log('running every minute to 1 from 5');
});
        
    

Using step values

Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: 1-10/2 that is the same as 2,4,6,8,10. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use */2.
    
    
var cron = require('node-cron');

cron.schedule('*/2 * * * *', () => {
  console.log('running a task every two minutes');
});
        
    

Using names

For month and week day you also may use names or short names. e.g:
    
    
var cron = require('node-cron');

cron.schedule('* * * January,September Sunday', () => {
  console.log('running on Sundays of January and September');
});
        
    
Or with short names:
    
    
var cron = require('node-cron');

cron.schedule('* * * Jan,Sep Sun', () => {
  console.log('running on Sundays of January and September');
});