Run a cronjob every day except the first day of the month

bleemboy asked:

I’m trying to:

  1. run job A the first day of the month: 0 0 1 * *
  2. run job B the other days of the month: 0 0 2-31 * ?

Vixie cron on Ubuntu 14.02 LTS refuses the second syntax, though it seems valid according to Wikipedia and official specs:

“crontab”, The Open Group Base Specifications Issue 7 — IEEE Std
1003.1, 2013 Edition, The Open Group, 2013, retrieved May 18, 2015

According to references above, the syntax 0 0 2-31 * * would run the job every day of the month as third and fifth fields are treated as OR clauses of the run condition.

My answer:


You should be using a *, not a ? (which is invalid).

The Wikipedia page notes that the ? is a nonstandard extension used only by nnCron, which you aren’t using.

In any case, if the day of week is set to * and the day of month is specified, then the day of week is ignored. The IEEE 1003.1 spec you reference actually states this, explaining how these fields interact:

If either the month or day of month is specified as an element or list, but the day of week is an <asterisk>, the month and day of month fields shall specify the days that match. If both month and day of month are specified as an <asterisk>, but day of week is an element or list, then only the specified days of the week match. Finally, if either the month or day of month is specified as an element or list, and the day of week is also specified as an element or list, then any day matching either the month and day of month, or the day of week, shall be matched.

So the correct format is exactly the logical one:

0 0 1 * * echo First of the month
0 0 2-31 * * echo Not the first of the month

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.