Cron Jobs On the Last Day of a Month
Some computer tasks needed to be performed on the first hour of the first day of the calendar month. Scheduling those tasks with cron is straightforward:
30 00 1 * * command
A related task needed to be performed before those tasks — in the last hour of the last day of the month. Cron does not support such a day. The last day could be 28, 29, 30, or 31.
In a shell script finding the last day of the month is possible with some if-then tests based on the memory aid Thirty Days Hath September. Determining whether the year is a leap year with 29 days in February makes the script a little longer.
That does not fit nicely into a cron job. Yet detecting the last day of the month is possible without a shell script using the date
command and a single test:
30 23 28,29,30,31 * * if [ $(date -d '+1 day' +%-d) -eq 1 ]; then execute command; fi
The cron job runs in the last hour of the day on the 28th, 29th, 30th and 31st of the month. If the date test returns a value of 1
then the current day is the last day of that month.
Posted: Usability Tagged: General
Category: