Reputation: 69
I want a certain function to run every round hour. There is the solution of running an interval when it's a round hour but I often turn on and off my script and I don't want to have to run it exactly on a round hour. I've tried looking through some npm modules and I found one but I had some issues with it. Does anyone have a solution?
Upvotes: 3
Views: 162
Reputation: 3561
No need for javascript! You have the perfect tool for that if you use linux!
Use cron:
$ sudo crontab -e
This will open a vim editor. Then add:
0 * * * * node /execute/your/script.js
(basically, it will run your code every hour on its minute zero)
More info
cron: https://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/
Upvotes: 3
Reputation: 138497
const HOUR = 1000 * 60 * 60;
function hourly() {
//....
setTimeout(hourly, HOUR);
}
setTimeout(hourly, HOUR - (new Date % HOUR));
Just calculate the next full hour when the server starts, and then shedule an hourly timer.
I admit that it might loose accuracy due to leap seconds :)
Upvotes: 1