'scheduling file every 1 hour using nodejs
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!');
});
Solution 1:[1]
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
Solution 2:[2]
Why can't you use setInerval()
setInterval(function () {
console.log('The answer to life, the universe, and everything!');
}, 1 * 60 * 60 * 1000); // 1 hour
Solution 3:[3]
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:
- Every 2 minutes:
*/2 * * * * - Every 2 hours when at 5 minute mark:
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:
- Linux crontab: at the minute level (allow 5 * in the config)
- node-schedule: at the second level (allow 6 * in the config)
Solution 4:[4]
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();
Solution 5:[5]
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
Solution 6:[6]
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!');
});
Solution 7:[7]
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');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh Patel |
| Solution 2 | mestarted |
| Solution 3 | |
| Solution 4 | Raja Rajan |
| Solution 5 | Ramesh Node |
| Solution 6 | |
| Solution 7 | Kari |

