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?


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...

  • It's interesting that even the BSD @ times don't include any "the last" cases -- I guess it's not a popular request (honestly I can't remember ever needing it myself)
    – voretaq7
    Mar 9 '10 at 16:25

I've never heard of this L construct, it's certainly not a standard that I'm aware of (definitely not in Vixie cron according to the man page & some empirical testing).

I wouldn't count on it working except on that one lone Wikipedian's machine, and I suggest using the method you describe in your question, which will work pretty much everywhere.

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.