Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Node.js Project to Build a Cron Job Task Scheduler Using Node-Cron Library in Javascript Full Tutorial

Posted on November 19, 2022

 

 

Welcome folks today in this blog post we will be building a cron job task scheduler in node.js using node-cron library in javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the below command as shown below

 

 

npm i node-cron

 

 

After installing the library make an index.js file for your node.js app and copy paste the below code

 

 

Now we will be executing the tasks every minute in node.js as shown below

 

 

JavaScript
1
2
3
4
5
var cron = require('node-cron');
 
cron.schedule('* * * * *', () => {
  console.log('running a task every minute');
});

 

 

As you can see we are importing the node-cron module and then we are using the schedule method to schedule the tasks to run every minute. These * represents the different sets of time available in node-cron module.

 

 

1
2
3
4
5
6
7
8
9
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *

 

 

Allowed values

 

field value
second 0-59
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names)
day of week 0-7 (or names, 0 or 7 are sunday)

 

 

Using Multiple Values

 

 

You can even provide multiple time intervals to run the node.js code using this cron module as shown below

 

 

JavaScript
1
2
3
4
5
var cron = require('node-cron');
 
cron.schedule('1,2,4,5 * * * *', () => {
  console.log('running every minute 1, 2, 4 and 5');
});

 

 

Or you can even provide the range values as well

 

 

JavaScript
1
2
3
4
5
var cron = require('node-cron');
 
cron.schedule('1-5 * * * *', () => {
  console.log('running every minute to 1 from 5');
});

Recent Posts

  • Angular 14/15 JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • Build a JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • React-Admin Example to Create CRUD REST API Using JSON-Server Library in Browser Using Javascript
  • Javascript Papaparse Example to Parse CSV Files and Export to JSON File and Download it as Attachment
  • Javascript Select2.js Example to Display Single & Multi-Select Dropdown & Fetch Remote Data Using Ajax in Dropdown
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme