Solved Cron syntax question

I've never needed to use cron before now, so its secrets remain a mystery to me. The handbook and man page appear to say (indirectly!) that '*' can mean either "all" or "not interpreted", and that conflicts are OR'd together. Do I have that right?

For example, to run a job at 0200 on the first of every month, I'd use

Code:
0  2 1 * *

whereas to run a job at 0330 every Sunday would require

Code:
30 3 * * 0

but

Code:
0 4 1 * 0

would run the job at 0400 if it's the first of the month OR it's a Sunday, rather than if it's the first of the month AND it's a Sunday.

And the same would be true, mutatis mutandis, for

Code:
0 4 */2 * sun

meaning run the job at 0400 every other day AND on every Sunday
 
You are right, the "*" does mean any match and the five time parameters must all match the current time for the job to be run. You used the example:

30 3 * * 0

Which, you're right, runs a job every Sunday at 3:30am.

Your second example

0 4 1 * 0

Is also correct, at least for most versions of cron, it will run on the first day of the month and on Sundays. However, I would not recommend doing it this way. Not all cron implementations will work this way so it might be better to get in the habit of making two entries like

0 4 1 * *
0 4 * * 0

It's a little more verbose, but easier to read and will work for other versions of cron other than FreeBSD's.
 
And the same would be true, mutatis mutandis, for

Code:
0 4 */2 * sun

meaning run the job at 0400 every other day AND on every Sunday
crontab(5) has the answer:
man 5 crontab said:
Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (ie, are not *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
 
Back
Top