Cron – L character in Cron Expression for “Last Day of the Month”

cronscheduled-taskunix

I'm currently writing an article on cron aimed at people who are familiar with the tool, but don't necessarily use it often. I want the examples to be compatible with most common Unix desktops in use (post 2002 linux, bsd, osx).

While looking for a cron expression for "last day of the month", most recommendations that I found suggest to use a combination of a cron expression that would run from the 28 to the 31 of each month combined with a command that includes a boolean date comparison.

TODAY=`date +%m` # Computes the month for today (01-12)
TOMORROW=`date --date=tomorrow +%m` # Computes the month for tomorrow (01-12)
0 0 28-31 * * [$TODAY -lt $TOMORROW] && command

This seems to be a popular Unix trick. I recently stumbled upon this wikipedia article on Cron Expressions that discusses some characters that I haven't seen mentioned anywhere else, not even the man pages. According to the article, the L character can be used in an expression to specify the last day of the month (or of the week).

Which version of cron supports this? how widely supported is it? why is it so little known?

Best Answer

I don't know about the L syntax, but on the BSD's, you can use:

       string     meaning
       ------     -------
       @reboot    Run once, at cron(8) startup.
       @yearly    Run every January 1, "0 0 1 1 *".
       @annually  (same as @yearly).
       @monthly   Run the first day of every month, "0 0 1 * *".
       @weekly    Run every Sunday, "0 0 * * 0".
       @daily     Run every midnight, "0 0 * * *".
       @midnight  (same as @daily).
       @hourly    Run every hour, on the hour, "0 * * * *".

But that is not standard either...