4

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.

5
  • 1
    Is that ? actually part of the spec you are trying to install ? – user9517 Sep 16 '16 at 13:33
  • 1
    This may be another hilarious Ubuntu regression; Fedora 24 accepts 0 0 2-31 * * /foo, as does Ubuntu 12.04 LTS. – MadHatter Sep 16 '16 at 14:01
  • @MadHatter the issue isn't with 0 0 2-31 * * but with 0 0 2-31 * ? – Marco Marsala Sep 18 '16 at 18:20
  • The ? is not in standard cron. And Wikipedia is not documentation. – Michael Hampton Sep 18 '16 at 18:43
  • @MichaelHampton I suspected that! Thank you for confirmation :-) Btw, before posting I searched for a while the man page explaining cron syntax but I couldn't find them. Where is that man page? – Marco Marsala Sep 18 '16 at 18:52
13

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
4

You could use date:

0 0 * * * [ $(date +\%d) -eq 1 ] && echo I will run on the 1st
0 0 * * * [ $(date +\%d) -ne 1 ] && echo I will run on all other days
4
  • 2
    You need to escape the % otherwise it will be treated as a newline. – user9517 Sep 16 '16 at 13:19
  • @Iain Good catch - forgot about that. Edited. – parkamark Sep 16 '16 at 13:20
  • any idea of why Vixie cron refuses this syntax? – Marco Marsala Sep 16 '16 at 13:23
  • I'm not sure but my hunch is that it is to do with the fact that there are not a constant number of days in each month across the year. – parkamark Sep 16 '16 at 13:26
0

If your crond doesn't allow you to specify the different dates, wrap a small shell script around your scripts.

#!/bin/bash
if [[ `date +%d` -eq 01  ]]
        then
                /path/to/script_a
        else
                /path/to/script_b
fi

Run the script every day and let it decide which of your scripts to run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.