ScanSkill
Sign up for daily dose of tech articles at your inbox.
Loading

How To Write Cron Job In Node.js

How To Write Cron Job In Node.js
How To Write Cron Job In Node.js

Let’s learn how to write a cron job in Node.js.

Introduction

Cron is a utility that allows you to run things on a regular basis. This is commonly accomplished with the cron syntax. We enable you to run a function whenever your scheduled job is triggered. Cron’s name originates from Chronos, the Greek word for time.

The cron command-line utility is a job scheduler on Unix-like operating systems. Now cron jobs are any tasks that run indefinitely during a set schedule. Cron Jobs are most commonly seen used for database dumps, backup, and furthermore for scheduling notifications. As a result of cronjobs being versatile different programming languages have their own way of implementing corn jobs. Let’s take a look at how to write a cron job in Node.js.

Prerequisites

Cron Job in Node.js

Firstly, let’s create a node application by installing all dependencies.

## Make a directory for project
$ mkdir node-scheduling

## Get into the project dirrectory
$ cd node-scheduling

## Initializing a node project
$ npm init --y
Initializing a node project
Initializing a node project

Now, that the node project is created, let us install the package that is used for running cron jobs. The package we are going to install is node-cron. This module allows you to schedule tasks in node.js using full crontab syntax. Now let’s install the package that will help us write the cron job in node.js.

## Install package for writing cron job in node
$ npm install node-cron
Installing node-cron package
Installing node-cron package

Now that the package is installed let’s create a index.js file, that will house a simple cron job.

## Creating index.js
$ touch index.js

Now, that the index.js file is created, let’s create a chron job that runs every second.

//* index.js

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

cron.schedule('* * * * * *', () => {
  const date = new Date();
  console.log(date);
});

The above cron job will print out the date every second until the program is terminated. Let’s run and test the code out.

## Running the cron job
$ node index.js
Cron job in node.js
Cron job running every second

We can see that the corn job ran every second, and printed out the date. We saw * asterisks used in the cron.schedule() method passed as an argument. The *‘s are used to signify time. The representation of what time it represents is given below.

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

For more detail on this look into the cron gurus website and basically play around with timing, and match it according to your needs.

Conclusion

So, in this article, we learned about how to write a cron job in node.js. We saw how patterns are matched and also looked at the general implementation areas of a cron job.

Sign up for daily dose of tech articles at your inbox.
Loading