thewatertower.org.uk

Crontab: the first five fields combine to restrict the schedule. Except for when they don't.

7.33.00pm GMT Mon 16th Mar 2009

Clock outside St Martin's Church, York. (photography: Matt Raines)

Mondays or the fifth of the month. Any one.

If you complete both the 'day of month' and 'day of week' fields, cron is likely to behave exactly the way you expect it not to.

First, a quick overview for reference. Crontab has six fields; the following .. plus the command to execute.

  • minute 0-59

  • hour 0-23

  • day of month 1-31

  • month 1-12 (or names

  • day of week 0-7 (0 or 7 is Sun, or use names)

Any of those fields can be replaced with an asterisk, which stands in for all potential values.

So, some examples:

  • 5 * * * * foo - 'foo' is executed at five minutes past every hour

  • 5 4 * * * foo - 'foo' is executed at 04:05 every day

  • 5 4 * * 1 foo - 'foo' is executed at 04:05 on monday

So far, so predictable. 'restrictions' are being applied, and the schedule is running less and less frequently.

So, lets say we wanted to run foo on the first Monday of every month. We might think that the following additional restriction would prevent the job running any other Monday:

  • 5 4 1-7 * 1 foo

We'd be really, really wrong. Cron will run foo every Monday *and* every day of the month from the 1st to 7th inclusive.

"If both [date] fields are restricted (i.e., aren't *), the command will be run when _either_ field matches the current time."

One question. Why?

It'd be so easy to put two cron tab entries in to effect the same behaviour. But oh no .. rather than making it intuitive and provide the additional functionality that 'AND' instead of 'OR' provides, they did it this way.

Not that I'm bitter.

Bookmark this story at: del.icio.usdel.icio.us DiggDigg FacebookFacebook LibDigLibDig redditreddit StumbleUponStumbleUpon
Print this news story
Comment on this news story
Previous news story: Changing the Outlook 2007 colour scheme (Sun 8th Feb 2009).
Next news story: The wonderful thing about release notes .. (Thu 23rd Apr 2009).

Printed and hosted by Prater Raines Ltd, 98 Sandgate High Street, Folkestone CT20 3BY.
Published and promoted by Ben Prescott, 14, St James's Square, Bournemouth, BH5 2BX. All rights reserved.
The views expressed are solely those of the author, not of the service provider.