Online Interview Questions

Prepare interviews on Anything, AnyWhere

PHPSCOTS,Blogger, Developer Views: 4075
Scheduling CRON Jobs on Node.js

Cron Jobs are used for scheduling tasks to run on the server.CRON Jobs are most commonly used method to automate tasks on Server. In this article, we will see how to schedule JOBS in Node.js. We are going to use node-schedule package for scheduling JOBS.

node-schedule is a light cron-like scheduler  Package for Node JS.

Installing node-schedule

npm install node-schedule

Setting up Scheduler

var scheduler = require('node-schedule');
var rule = new scheduler.RecurrenceRule();
rule.hour = 7
rule.dayOfWeek = new schedule.Range(0,6)
var dailyJob = schedule.scheduleJob(date, function(){
 console.log('I run on days at 7:00');
});

scheduler.scheduleJob(rule,dailyJob);

Canceling a Scheduled Job on Node

dailyJob .cancel();

You can read more about node-schedule package from https://github.com/node-schedule/node-schedule

Also Read Node js interview questions

2 thoughts on “Scheduling CRON Jobs on Node.js”

Leave a Reply

Your email address will not be published. Required fields are marked *