Kaushik Makwana
Kaushik Makwana

Reputation: 2576

runSchedule job every hour in nodeJS

i'm working with nodejs and Mongodb. i want to run job every user define hours using npm node-schedule but its did't work.

Here is my code

    var schedule = require('node-schedule');
    var  mailTimer = 1;
    var intMail = schedule.scheduleJob('* * '+mailTimer+' * *', function(){
          console.log('its run');
    });

//its means its run every 1 hour.

Upvotes: 3

Views: 2852

Answers (3)

SiddAjmera
SiddAjmera

Reputation: 39482

Try this :

var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.hour = 1;
var intMail = schedule.scheduleJob(rule, function(){
      console.log('its run');
});

You can use other rules to set like :

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

Here's another example using other rules:

var j = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function(){
    console.log('Time for tea!');
});

More examples can be found here

Upvotes: 6

Parthapratim Neog
Parthapratim Neog

Reputation: 4478

There needs to be one more * at the end of the input string if I'm not wrong. Add it and check the response. For one hour you can use * * 1 * * *

Upvotes: 1

Lewis
Lewis

Reputation: 14926

The crontab config should be 0 */'+mailTimer+' * * *.

Upvotes: 1

Related Questions