Issue
I would like to run a cronjob every 5 hours.
Now it's 11:54pm..
Checking the crontab guru, this 0 */5 * * *
seems to be correct, but the site also mentions something like this
next at 2022-01-07 00:00:00
I would like to know why? Does it mean it runs the script no matter what at midnight(00:00:00) ? Doesn't make sense.
Solution
The */5
syntax means "every 5 units, starting from 0". So, if you use it in the hour position, it will match hours 0, 5, 10, 15, 20.
I'm assuming what you actually want is a strict 5 hour interval. So after hour 20, you want the next run to be at 20 + 5 hours, so at 1AM not midnight. If that's correct, there isn't an easy way to make cron work like that. It can do even intervals of all divisors of 24 though: every 2 hours, every 3 hours, every 4 hours, every 6 hours, every 12 hours.
To get the 5 hour interval, one possible workaround is to
- schedule the cron job to run every hour
- at the start of your script that the cron job runs, add extra logic to check if it should run this hour. For example, you could take the current time, and calculate the hours since epoch. And exit early unless the calculated number is divisible by 5.
Answered By - PÄ“teris Caune Answer Checked By - Katrina (WPSolving Volunteer)