Running cron jobs with node.js

Persis Randolph
3 min readJul 1, 2021

--

Photo by Kevin Ku on Unsplash

What are cron jobs?

Cron jobs are a way to schedule repeated tasks, mostly used by system administrators. Crons are a background process (daemon) that you can set up to run commands on a schedule. Automating tasks like monitoring disk space, performing system maintenance, backing up files, backing up databases, or running any other type of script can also be greatly beneficial for a busy dev.

Let’s do it!

A cron is usually a text file, but today we’ll be using a dependency, node-cron, to simplify the process.

Install cron in your project’s root directory with the following script.

npm install cron

You’ll need to have a server running where you can set up your cron job. In the example below I’ve set up a simple server using express. This is my index.js file.

const express = require('express');const app = express();
const PORT = 8080;
app.listen(`localhost:${PORT}`, `cron-demo listening on port ${PORT}`);

I’ve set up another file for the cron job. This is cron.js.

const { CronJob } = require('cron');console.log('this will log before job instantiation');
const job = new CronJob('* * * * * *', () => {
console.log('cron is executing now. timestamp: ', new Date());
})
console.log('this will log after job instantiation');
module.exports = { job };

Let’s break down this file so you know what’s happening. Here, we require our necessary dependency, cron.

const { CronJob } = require('cron');

When your server boots up, we’ll run the cron job (it isn’t set up in app.js yet, we’ll do that a few steps down the line). You’ll be able to tell the job is starting with this line.

console.log('this will log before job instantiation');

This is where we’ll define the new cron. Here you see the keyword new used in conjunction with CronJob to invoke a new instantiation of a cron job. The first parameter will be six asterisks, or number values in their place, depending on how you’d like to schedule the cron.

Six asterisks means that this job will run every second, of every minute, of every hour, of every day of the month, of every month, on every day of the week.

These are the ranges as defined by the node-cron documentation:

  • Seconds: 0–59
  • Minutes: 0–59
  • Hours: 0–23
  • Day of Month: 1–31
  • Months: 0–11 (Jan-Dec)
  • Day of Week: 0–6 (Sun-Sat)

For each asterisk, you can replace it with the corresponding value from the above ranges.

const job = new CronJob('* * * * * *', () => {
console.log('cron is executing now. timestamp: ', new Date());
});

If our cron works correctly, you should be able to see a console.log statement every second with a current timestamp.

Next, we see a log that shows you the job instantiation has been completed and we also export the job to be used elsewhere.

console.log('this will log after job instantiation');module.exports = { job };

Now, let’s run the cron by importing it and using it after starting up our server. Back in index.js, let’s add a line to import, and to call the newly created cron job.

const express = require('express');
const { job } = require('./cron'); // require the job
const app = express();
const PORT = 8080;
app.listen(`localhost:${PORT}`, `cron-demo listening on port ${PORT}`);
job.start(); // here we instantiate the cron job

When you start up your server you should now see the console.log statements coming from the cron job every second. It should look something like this:

➜  cron-demo node index.js
this will log before job instantiation
this will log after job instantiation
cron is executing now. timestamp: 2021-07-01T03:26:15.003Z
cron is executing now. timestamp: 2021-07-01T03:26:16.003Z
cron is executing now. timestamp: 2021-07-01T03:26:17.000Z
cron is executing now. timestamp: 2021-07-01T03:26:18.002Z
cron is executing now. timestamp: 2021-07-01T03:26:19.001Z
cron is executing now. timestamp: 2021-07-01T03:26:20.004Z
cron is executing now. timestamp: 2021-07-01T03:26:21.000Z
cron is executing now. timestamp: 2021-07-01T03:26:22.000Z
cron is executing now. timestamp: 2021-07-01T03:26:23.004Z

Next, I’d recommend playing around with some different time ranges. See if you can get the cron to run every 20 seconds or every few minutes. There are lots of examples in the documentation in case you want to run a command each day at midnight, or even simply once a month!

--

--

Persis Randolph
Persis Randolph

Written by Persis Randolph

Full-Stack Developer based out of New Orleans, LA

No responses yet