Hacker News new | ask | show | jobs
by TylerE 1411 days ago
My favorite cron “feature” is that the day of week field operates acts as an “or” and not an “and”.

This will come as an unpleasant surprise when you try to schedule something to run on, say, the first Friday of the month.

There are workarounds, mostly involving backticked date invocations, but they’re hard to both write and read.

2 comments

In Debian cron, you can do:

0 0 */50,1-7 * FRI

It works, but it's a bit of a hack (to put it mildly ;-) )

Consider this expression:

0 0 1-7 * FRI

This one will run on dates 1-7, and additionally on every Friday. This is almost what we want, except we want an "AND" relationship between the day-of-month and the day-of-week fields, instead of the "OR" relationship.

The weird extra */50 bit exploits a quirk in Debian cron's expression parsing logic. It fools cron into thinking the day-of-month field is a wildcard field, and into applying the "AND" logic.

Are you sure? The man page for Vixie cron at least doesn't imply that and it's not what I remember, and the web page this links to also doesn't think that's true. So "First Friday of the month at midnight" gives "0 0 1-7 * 5".
From man 5 crontab

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 (i.e., aren't ), 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.

Aside from the manpage, you can also check ones you're unsure of on crontab.guru - https://crontab.guru/#0_0_1-7_*_5