Scheduling CRON Jobs on Node js using node schedule

Sharad Jaiswal
Written by Sharad Jaiswal

Posted On: Dec 30, 2018

Scheduling CRON Jobs on Node js using node schedule

 

Cron Jobs are used for scheduling tasks to run on the server.CRON Jobs are the 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

    Please Login or Register to leave a response.

    Related Articles

    Node Js Tutorials

    How to enable cors in Node js

    In this article, we are going to see how to enable CORS ( Cross-Origin Resource Sharing ) in Node JS. CORS essentially means cross-domain requests. Cross-Origin Resource Sharing (CORS) is a mechanism ..

    Node Js Tutorials

    Uploading image from url using Node js

    In this tutorial, we are going to see how to Upload an image from URL using Node Js. We have written a simple function In Node.js to save an image from URL to local disk/ server. Whenever we use login..

    Node Js Tutorials

    Dropping an existing index from MongoDB or mongoose

    While working with MongoDB and Node js I face an issue. I have created mobile_no unique in starting and after some time I realized that I don't need my mobile_no to be unique. so I just go to my mode..

    Ask a Question