cron job that runs on the last monday of the month

Solution 1:

Your cron job wil run on a Tuesday (5th field, day of week, 0 or 7 is Sunday), and not always the last Tuesday of the month. It would be better to use:

8 04 * * 1 [ $(date +"\%m") -ne $(date -d 7days +"\%m") ] && $HOME/bin/mailstub

This checks if the month today is not equal to the month 7 days from now. If not equal then it's the last monday of the month.

Solution 2:

That is setup to go multiple times per day. You want:

0 8 04 19-26 * * MON $HOME/bin/mailstub

That will go off at 8:04:00 on any day between the 19-26 that is on a Monday of any month of any year.

However, if it is Monday on the 19th, it is monday on the 26th. Therefore, you would want to have it from the 19-25

0 8 04 19-25 * * MON $HOME/bin/mailstub

So, this should go off once per month on a Monday. However, it is not guarenteed (or likely) to be the last Monday. If you need it to be the last and the L syntax does not work, you need to program the logic into the script being called. This is easily done with python but is beyond the scope of this question.

Tags:

Cron