ChatGPT解决这个技术问题 Extra ChatGPT

Run Cron job every N minutes plus offset

*/20 * * * *

Ensures it runs every 20 minutes, I'd like to run a task every 20 minutes, starting at 5 past the hour, is this possible with Cron? Would it be:

5/20 * * * * ?

Thanks the Babylonians, who decided that our time system should be exactly divided by 1,2,3,4,5 and 6.
if it's divisible by 2 and 3 it's also divisible by 6

t
toxalot

To run a task every 20 minutes starting at 5 past the hour, try this:

 5-59/20 * * * *

Explanation

An * in the minute field is the same as 0-59/1 where 0-59 is the range and 1 is the step. The command will run at the first minute in the range (0), then at all successive minutes that are distant from the first by step (1), until the last (59).

Which is why */20 * * * * will run at 0 minutes, 20 minutes after, and 40 minutes after -- which is the same as every 20 minutes. However, */25 * * * * will run at 0 minutes, 25 minutes after, and 50 minutes after -- which is not the same as every 25 minutes. That's why it's usually desirable to use a step value in the minute field that divides evenly into 60.

So to offset the start time, specify the range explicitly and set the first value to the amount of the offset.

Examples

5-59/20 * * * * will run at 5 minutes after, 25 minutes after, and 45 minutes after.

10-59/25 * * * * will run at 10 minutes after and 35 minutes after.

1-59/2 * * * * will run every odd minute.


that works perfectly. How did you find that out? Can you point me towards the docs? Thanks.
A variety of Google searches led me to this Server Fault answer. It had a mistake which had me really confused, especially since other results suggested that minutes modulus step should equal zero. I finally found this manual page which gave me the answer. As a result, I was able to comment on the Server Fault answer which has now been fixed. I don't remember the actual search terms I used to find the IBM page.
Thanks and very good work @toxalot, I wish we could award you the "Tireless Investigator" badge! :P
LOL. I needed to run a job every 10 minutes (offset by 5) and really did not want to use a comma delimited list, so I was determined to find the answer.
CPanel doesn't seem to like this unfortunately :(
f
fedorqui

Sure!

5,25,45 * * * * /your/cron

Thanks. I had ended up doing this, was hoping there might be a more elegant solution to it.
Sometimes the simple solution is better because it's easy easy to read and understand. I like knowing that 5-59/20 is possible, but 5,25,45 has the advantage that it's immediately very clear what that does.
only if your step is big enough
This is a much better example. It takes 0 cognitive load to understand when the job kicks off.
D
Dan Is Fiddling By Firelight

You can try: */5 * * * * sleep N; your job


I see nothing wrong with this - in fact this is very similar to how certbot's cron job achieves a random offset which helps keep server load balanced. sure doesn't deserve all those downvotes
This made me wonder how to sleep a random number of seconds. sleep `shuf -i 1-100 -n 1` does the trick.
dammit, @Laizer you made me learn something again! (thank you!)