crontab weirdness … schedule a job to run on first saturday of every month

We wanted to schedule a backup script to run on the first saturday of every month at 13:10 on a debian lenny host.

The syntax we used in crontab was:
10 13 1-7 * 6 /path/to/myscript.sh

But because of the cron-implementation the script was running on every saturday AND on the 01.-07. of every month.
This is not a bug, but a “feature” – I found the following documentation on wikpedia http://en.wikipedia.org/wiki/Cron:

if both “day of month” and “day of week” are restricted (not “*”), then either the “day of month” field (3) or the “day of week” field (5) must match the current day (even though the other of the two fields need not match the current day).

The resolution for me was to enforce the time check in the command line:
10 13 * * 6 if [ $(date +\%d) -lt 7 ]; then /path/to/myscript.sh; fi

Leave a Reply

Your email address will not be published. Required fields are marked *