JavaScript

How To Run Scheduled Jobs in Node.js using node-cron

When you are running a server or an industrial-level application, you want that server to manage future jobs as well. And if you are familiar with programming, you should know that the server can’t do anything until we tell it to do something. This simply means that if you want the server to do something in the near future then you need to program it in such a way that it performs a specific task at a later time.

Node packages library includes a lot of packages and modules that are used to run scheduled tasks, but the most famous and the most widely used package is the cron package which is also called the “nodecron” module. The cron offers a syntax that allows the programmer to execute a specific task at equal intervals. You can use this module or cron to schedule sending emails on a specific date and at a specific time, or you can run a task every minute

To run the cron on your server and to follow this post you need to have NodeJs up and running on your local machine or on your server.

Step 1: Set up the environment

To demonstrate the use of node-cron and to explain the syntax of cron, we are going to create a new node project and write some code that will execute a job every minute.

To do this, let’s first create a new directory using the following command in the terminal:

mkdir testing-nodecron

We created a new directory, now to head inside that directory we need to use the following command:

cd testing-nodecron

The next step is to set up our package.json file and install dependencies, for that we need to call the node innit using the following lines of code:

npm init -y

Lastly, to install the node-cron module in our node project we use the following command in the terminal:

npm install node-cron

This will install our task scheduler and you will see the following result in the console:

Step 2: Making a scheduled task job

After we have set up our environment, we can move on to using the node-cron package to write some server code that will execute a certain task after every minute. To do this we need to create a new file and we will call this file perMinuteTask.js:

Inside this file, the first thing that we need to add is the node-cron library with the following line:

const cron = require("node-cron");

Now, the node-cron module has a function called schedule() which takes in 2 arguments

  • The first is the time stamp denoted by a syntax of asterisks
  • The second one being the function that will be executed at a specific time

We can use this function, to run a task at every minute using the following lines:

cron.schedule('* * * * *', function() {

console.log('New task every minute');

});

The syntax of asterisks can be denoted as

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *

Looking at the syntax, we used 5 asterisks meaning that the function will be called every minute till the end of time (metaphorically)

Step 3: Executing the code and testing the output

The last step is to run this code and examine the output of the program. To run this code, type in the following command in the terminal:

node perMinuteTask.js

At first terminal may look empty like this:

But after waiting for a few minutes, you will get the following result on your console:

As this program has no ending condition, so if you want to close this program you need to press (Control + C) to manually stop the execution

And there you go, you have successfully learned how to run scheduled tasks in NodeJs using the node-cron module

Conclusion

Cron, in reality, is a command-line tool for UNIX operating system that helps the user to schedule a job to be executed at a later time or to run a task after a set interval of time has elapsed; You can create a simple job scheduler in NodeJs by using its node-cron module. In this post, we learned about the node-cron module and how to install\use it in our node project. We learned how to run a task every minute automatically using the node-cron module.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.