What is a cron job and how to create one in Node.js

A cron job is a scheduled task that runs automatically at specified intervals on Unix-like operating systems. These tasks are commonly used to automate repetitive tasks, such as data backups, log rotations, and various maintenance tasks. Cron jobs are managed by the cron daemon, a background process that executes scheduled commands at predetermined times.

Creating a cron job in Node.js

The following steps show us how to create a cron job in Node.js:

Step 1: Install the node-cron package

To create a cron job in Node.js, we can use the node-cron module. This module provides a simple API for scheduling tasks and is easy to use. To use it, first, install it using npm:

npm install node-cron

Step 2: Importing node-cron in a file

Import the node-cron module in the file where we want to create the cron job.

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

Step 3: Define the function to be executed

Define the function that we want to execute at specific intervals:

const fun = () => {
// Your task or function logic here
console.log('Cron job executed!');
};

Step 4: Schedule the cron job

Once we have defined the function, we can use the cron.schedule() method to schedule our function at a specific time or interval. The syntax for the cron expression is as follows:

cron.schedule('second minute hour day month dayOfWeek', fun);

For example, to run the function (fun) every minute:

cron.schedule('* * * * *', fun);

The cron expression, in this case, is * * * * *. This expression means the job should run every minute, on the hour, day, month, and year. In cron expressions, each symbol serves a specific purpose in describing the desired execution time of a job:

  • An asterisk (*) signifies “every interval.” For instance, if placed in the “month” field, it implies the task runs every month.

  • The comma (,) enables us to specify a list of values for repetition. For example, if we use 1, 3, 5 in the “month” field, the task will run in months 1, 3, and 5 (January, March, and May).

  • The hyphen (-) allows us to define a range of values. If, for instance, we input 1-5 in the “day of the week” field, the task will run every weekday (from Monday to Friday).

  • The slash (/) lets us express “every xth interval.” For instance, if */4 is in the “hour” field, the action will be performed every four hours.

Meaning of * in the cron job
Meaning of * in the cron job

The syntax comprises of six fields with the following potential values:

  • Seconds (optional): This field specifies the second the command will execute within a minute, ranging from 0 to 59. For example, if we want the cron job to run every 10 seconds, we can write it like cron.schedule('*/10 * * * * *', fun). This field can be left out. In this case, the cron expression will only consist of 5 elements, and the first element describes the minutes and not the seconds.

  • Minute: This field specifies the minute the command will execute within an hour, ranging from 0 to 59.

  • Hour: It represents the hour when the command will run, using a 24-hour notation, from 0 to 23.

  • Day of the month: This field indicates the specific date within the month when the user wants the command to execute, ranging from 1 to 31.

  • Month: It signifies the month when the command will run, from 1 to 12, corresponding to January through December.

  • Day of the week: This field defines the day of the week when the command will execute, ranging from 0 to 6. The values represent Sunday through Saturday, with some systems using 7 to represent Sunday.

If we don’t have a specific value for a field, it’s essential to avoid leaving it blank. Instead, use an asterisk (*) to indicate that any value is acceptable. For instance, if we want to execute the function fun every Saturday at 4:10 p.m., we would use the following cron command:

const cron = require('node-cron');
cron.schedule('10 16 * * 6', fun);

In this example:

  • 10 and 16 denote 4:10 p.m.

  • The asterisks in the “date” and “month” fields signify that the task should run on any day and in any month.

  • The 6 designates Saturday, indicating that the task follows this schedule.

Step 5: Save and Run

Once our cron job is saved in the file, we’ll need to activate the node-cron scheduler. We can achieve this by executing the following command:

node cron.js

Now, the node-cron scheduler will execute our cron job at the specified time or interval.

If we want to incorporate multiple cron jobs within a single Node.js application, it's straightforward. We can create additional cron job files as needed and then schedule them using the cron.schedule() method.

Code example

Following is the executable code example of a cron job:

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

// Cron job that runs every 5 seconds
cron.schedule('*/5 * * * * *', () => {
  console.log('Cron job executed.');
});

console.log('Cron job started.');
Cron job in action

Cron jobs represent a potent utility for automating operations in Node.js. Employing the node-cron library, we can conveniently set up tasks to execute at designated times or intervals. This automation can be a valuable resource, enabling us to allocate our time to other priorities and responsibilities.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved