332

*/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 * * * * ?

AJP
  • 26,547
  • 23
  • 88
  • 127
  • 11
    Thanks the Babylonians, who decided that our time system should be exactly divided by 1,2,3,4,5 and 6. – arivero Apr 19 '15 at 22:40

3 Answers3

688

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.

toxalot
  • 11,260
  • 6
  • 35
  • 58
  • 8
    that works perfectly. How did you find that out? Can you point me towards the docs? Thanks. – AJP Oct 07 '13 at 14:38
  • 27
    A variety of Google searches led me to [this Server Fault answer](http://serverfault.com/a/429397/181709). 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](http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp?topic=%2Fcom.ibm.db2.luw.sql.rtn.doc%2Fdoc%2Fc0054381.html) 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. – toxalot Oct 07 '13 at 18:44
  • 8
    Thanks and very good work @toxalot, I wish we could award you the "Tireless Investigator" badge! :P – AJP Oct 07 '13 at 23:39
  • 7
    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. – toxalot Oct 08 '13 at 04:04
  • Is it possible to do an offset in seconds? Run every 5 minutes + 10 seconds? – Bobby S Aug 04 '17 at 22:40
  • 2
    CPanel doesn't seem to like this unfortunately :( – Robert Went Sep 19 '17 at 20:23
  • With this expression 0 57-59/15 * 1/1 * ? , the job will only start once (00:57) in an hour and not four times like I want it to (00:57,01:12,01:27,01:42). I want the user to be able to start the job every 15 minutes starting at a specific minute like in this case an offset of 57 minutes but the job will only execute within the range of 57-59 using your solution. The solution I found is the following : When the offset is greater than the step, I subtract the offset by the quotient of (Offset / Step) * step. In this case : 57 / 15 = 3, 3*15=45, so 57-45=12. The cron will be 0 12/15. – jbrabant Nov 07 '18 at 14:39
  • @BobbyS There's just the hacky way suggested by LeoChu's answer, which is to add `sleep 10;` to the beginning of your command. – Mike Nov 14 '18 at 14:15
  • I've written a bit on top of this to illustrate some more patterns https://gist.github.com/timkpaine/09386bb62673d0fd38a76856a1dcb8e3 – Tim P Jul 25 '19 at 23:57
  • Here's a useful website to test various settings: https://crontab.guru/ – jlh May 24 '20 at 10:24
  • 1
    Linked docs no longer exist. But `man 5 crontab` is what you want, e.g.: https://linux.die.net/man/5/crontab – jessewmc Aug 20 '20 at 23:48
  • I am impressed at @toxalot's mad google skillz and I am flabbergasted by the stupidness of the cron system. If you have the chance (I guess that means runit instead of systemd), use [snooze](https://github.com/leahneukirchen/snooze). – BUFU Mar 22 '21 at 21:44
  • This solution doesn't work for me on my scheduler. It says cron expression is invalid required 6 fields but found 5. – Hetal Rachh Jan 25 '22 at 07:53
87

Sure!

5,25,45 * * * * /your/cron
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 3
    Thanks. I had ended up doing this, was hoping there might be a more elegant solution to it. – AJP Feb 27 '13 at 18:22
  • 24
    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. – thomasrutter Jan 07 '16 at 04:52
  • 5
    only if your step is big enough – Tebe Feb 08 '17 at 11:36
  • 6
    This is a much better example. It takes 0 cognitive load to understand when the job kicks off. – Dylan Pierce May 17 '18 at 16:50
18

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

Leo Chu
  • 399
  • 2
  • 5