Cron Job(A.K.A. Job Scheduling) With Nodejs

Job Scheduling is the important part of development when you want to make some things that can work with certain amount of period.

It is not compulsory to use nodejs but in this article i will be using nodejs to make it work.

Some of the examples of cron job are like,

  • Send Bulk email at certain time
  • Daily Database backup
  • Generating Invoices automatically

and there are lot more as per the requirement of our project.

So let's start with step by step.

In order to use it in nodejs we have multiple npm packages like, cron, node-cron, But we will use cron.

So first go to your terminal and write following command.

npm init -y

It will create package.json file and now we can start install our dependencies.

Now we need to type following command to install cron for nodejs

npm install node

It will install that package for use and you can see the dependency will be listed in package.json file.

Now make a javascript file and give the name whatever you want to give, I will call it app.js.

Now write following code in app.js

const CronJob = require('cron').CronJob; //importing cron job package in our file

var job = new CronJob('* * * * * *', function(){
     const d = new Date();
    console.log('This job will run every second:', d);
},function(){
  console.log('Job Stopped');
},false, "US/Eastern");

//To start the job
job.start();

Here we have initialised the job and then we called the start method to make it work. As you can see that there are some arguments in CronJob function like ' ', two callback function and US/Eastern.

So first argument is the time we have defined, there are six stars in the argument.Every star denotes the time.

Let's see some example,

  • First star denotes seconds

  • Second star denotes minute

  • Third star denotes hour

  • Fourth star denotes Day of Month

  • Fifth star denotes Months

  • Sixth star denotes Day of Week

Confusing Right? Let's take one by one

See the code below

const job = new CronJob('* */10 * * * *', function() {
    const d = new Date();
    console.log('This will run in every ten minutes:', d);
});

Above job will run every ten minutes.

const job = new CronJob('*/20 * * * * *', function() {
    const d = new Date();
    console.log('This will run in every twenty seconds:', d);
});

Above job will run in every twenty seconds.

const job = new CronJob('* 10 * * * *', function() {
    const d = new Date();
    console.log('This will run at Ten Minutes:', d);
});

Above job will run at ten minutes of hour.

Note - Above job will run only one time in every hour and other job is every ten minute mins it will run every ten minutes of an hour.

(' /10 ') -> Every Ten minute of every hour.

(' 10 *') -> At ten minute of every hour.

More Examples

const job = new CronJob('* * 6 * * *', function() {
    const d = new Date();
    console.log('This will run at Ten Minutes:', d);
});

Above job will run every day at 6 PM IST(India Standard Time)

const job = new CronJob('00 00 00 * * *', function() {
    const d = new Date();
    console.log('Every Midnight:', d);
});

Above Job will run every Midnight.

So now you are clear with timing of job that how we can configure the time.

In Next Blog I will share that how we can send the bulk emails everyday.Till then stay tuned.

Thank for reading.