Reputation: 105
I want to run a print statement every 1 hour in Windows environment using node.js. I use the package node-schedule.
But when I tried running this, the output is not as expected. I think my schedule format is wrong.
So I tried the following code:
var schedule = require('node-schedule');
var j = schedule.scheduleJob('*/1 * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Upvotes: 10
Views: 28312
Reputation: 1
it will run every hour at 0 min, 0 secs.
const job = schedule.scheduleJob('0 0 */1 * * *', function(fireDate){
console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
});
Upvotes: 0
Reputation: 25050
You can try this:
const schedule = require('node-schedule');
const job = schedule.scheduleJob('1 * * * *', () => { // run every hour at minute 1
console.log('The answer to life, the universe, and everything!');
});
More examples:
*/2 * * * *
5 */2 * * *
You should use this friendly tool to read and verify the config. Example:
It's worth noting that not all cronjob libs support the same fine-grain time level. For example:
Upvotes: 5
Reputation: 1360
This runs when "second is zero" and "minute is zero", and "any hour", "any day", "any month", "any day-of-week". Basically, every hour.
schedule.scheduleJob("0 0 * * * *", function() {
console.log('I run every hour');
}
Upvotes: 0
Reputation: 334
You need to add one more "*" to the cron-style. They do have a good README.md if you want to find out more about it.
const schedule = require('node-schedule');
const job = schedule.scheduleJob('* */1 * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
The cron format consists of:
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ 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)
EDIT
If you would like to run it once every hour on a specific minute, you can also do this and switch the 0 with any minute 0-59:
const schedule = require('node-schedule');
const job = schedule.scheduleJob('0 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Upvotes: 0
Reputation: 91
This will run every 1 hour, 0th minute, 0th second( ex: execute at 1PM, 2PM, 3PM etc):
var CronJob = require('cron').CronJob;
var job = new CronJob('0 0 */1 * * *', function() {
console.log('The answer to life, the universe, and everything!');
});
job.start();
Refer: https://www.npmjs.com/package/cron
Upvotes: 1
Reputation: 10254
var schedule = require('node-schedule');
var j = schedule.scheduleJob('* 1 * * *', function(){ // this for one hour
console.log('The answer to life, the universe, and everything!');
});
The cron format consists of:
* * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ 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 is the link: https://github.com/node-schedule/node-schedule
Upvotes: 11
Reputation: 1
This will run by based on your time zone, for example, Now the time in your time is 12:30, your scheduler will run by 01:00.Please check
Upvotes: 0
Reputation: 413
Why can't you use setInerval()
setInterval(function () {
console.log('The answer to life, the universe, and everything!');
}, 1 * 60 * 60 * 1000); // 1 hour
Upvotes: 4