view raw
Syed Asad Abbas Zaidi Syed Asad Abbas Zaidi - 10 months ago 24
Node.js Question

Schedule task for every 4 hours in Node.js

How could I schedule a task to run after 4 hours using "node-schedule" in Node.js
Currently my code is as below but it isn't responding as expected.

var schedule = require('node-schedule');
var task = schedule.scheduleJob('* */4 * * *', function () {
console.log('Scheduled Task');

Syc Syc

Your syntax creates a cron that runs every minute every 4 hours.

The syntax you are looking for is 0 */4 * * *. Wich executes ONCE every 4 hours.

You can test the cron syntax with the website

Another option setting your cron in node is using rules. See

var rule = new cron.RecurrenceRule();
rule.hour = 4;
rule.minute = 0;
cron.scheduleJob(rule, function(){
    console.log(new Date(), 'Every 4 hours');