How to use Vixie cron to schedule at regular odd times

April 3, 2009

The traditional way of specifying that cron should run a command every ten minutes is to write out a list of every minute that the command is to run on:

0,10,20,30,40,50 * * * * /some/command

Vixie cron introduced a shorthand notation for this: '*/10'. We use it quite a lot, since it is very convenient; it is both shorter and more explicit about what is really going on, which means that we are less likely to make mistakes.

(Quick, spot the error in '10,20,30,40,50'. Or is there even an error? Without additional information on what was actually intended, you can't know for sure.)

But suppose you want to run a command every ten minutes but offset by two minutes (so that it runs at :02, :12, and so on) because, for example, you already have another command that runs every ten minutes and you don't want the two to clash. It turns out that Vixie cron also has a short form for this, one that I find somewhat less obvious: '2-59/10'.

(Per the documentation, this means 'every ten minutes starting at :02 and going to :59'; the '2-59' is the range and the '/10' is the skip interval. Cron starts at the start of the range and then skips forward the skip interval every time until it runs out of the range.)

You have to start the range at the offset that you want, in this case two minutes, but the end time doesn't have to be aligned with the skip interval. I end my ranges at 59 minutes by convention, because it's always good enough regardless of what the skip interval is.

(This is one of those entries that I write so that maybe I'll remember the logic the next time around, and if not I can look it up here. Although the manpage actually is quite clear if read carefully, now that I look at it closely.)

Written on 03 April 2009.
« System status announcements and where your users are
The (or a) problem with Unix manpages »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Fri Apr 3 00:23:28 2009
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.