Reputation: 1
I tried using both npm packages "cron" and "node-schedule" to make this work, but i couldn't. Let's say i want to start the cron job at 6.30PM, and starting from 6PM i want it to be executed every 2 hours. First is that i couldn't make it work if it is not an exact hour , meaning if it is not (1PM,2PM,8PM.....) So i supposed that it will run at 6PM sharp and not 6.30PM I tried this pattern: "0 18-23/2 * * *" which is supposed to work from 18 to 23, escaping two hours [18,20,22,00] but once it's past midnight it will stop til it's 18h again. i want it to keep executing every 2 hours (meaning it executes at 2AM and so on ...) IS that possible with cron jobs ?
Upvotes: 0
Views: 1183
Reputation: 71
You could do a basic shell script to check if the current date and time are greater than or equal to the date and time you specify before executing the command. Please note that the date and time are specified in UTC. This will cause the cron to run every 2 hours 30 minutes past the top of the hour but it wont actually activate until after 6PM December 3rd 2019 UTC. Also note that the date command may vary with different flavors of nix* / BSD / OSX.
30 */2 * * * if [ $(date --utc +%s) -ge $(date --utc --date "2019-12-03T18:00:00" +%s) ]; then (command) fi
Upvotes: 1
Reputation: 70528
I believe you are talking about using crontab to schedule jobs.
crontab is designed to let you set the frequency based on many things but it is not designed to become "enabled" a certain time.
so the following will run every two hours
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * (command)
Lets say it is 1st of the month and you want it to run every two hours starting at 6 pm I would do the following
0 18,20,22 1 * * (command)
0 0,2,4,6,8,10,12,14,16,18,20,22 2 * * (command)
This means that today (based on day of month it would run starting at 6 and tomorrow it will run every two hours. Then sometime tomorrow I would edit the file and remove the first line and change the '2' for the day of month to a '*' (to match the first line I showed you) -- now it will always run every two hours.
tldr; edit crontab to work for two days and then edit it after the go live to work for every day.
Upvotes: 0