Cron on the Last Day of the Month: Why 31 Is Not Enough
Month-end jobs look easy until the first short month. Invoices, statements, backups and reports all want the same thing: run once, on the last day, whatever day that is. Cron has no symbol for it, and the obvious line is wrong.
The line everyone writes first
0 0 31 * * /usr/local/bin/month-end.sh
That line runs on the 31st. Months with thirty days never reach it, so the job is skipped in April, June, September and November. February is skipped every year. The result is seven runs instead of twelve — and nobody notices until a statement is missing.
Why cron cannot express it
Cron counts values, not positions. The last day of a month is a moving target: 28, 29, 30 or 31 depending on the month and the leap year. Every field in a cron line holds a fixed list of numbers, so “the last one that exists this month” has no place to live.
The same limit hits “the last weekday of the month” and “the third Thursday”. Cron can name a range; it cannot name the end of a range that changes.
Fix 1: run daily, check tomorrow
The standard fix is to run every day in the tail of the month and let the command decide whether today is the last day:
59 23 28-31 * * [ "$(date -d tomorrow +\%d)" = "01" ] && /usr/local/bin/month-end.sh
Read it in three parts. 28-31 limits the attempts to the four days that could be a month end. The bracket test asks the system what tomorrow’s day number is. If tomorrow is the 1st, today was the last day, and only then does the script run.
Two details make or break this line:
- The percent signs are escaped. In a crontab, an unescaped
%turns into a newline, which cuts the command in half. Inside the date format, write\%d. - The date command is GNU.
date -d tomorrowworks on Linux. On macOS and BSD the same idea isdate -v+1d +\%d. If the job runs on both, put the test inside a small script instead of the crontab line.
The 23:59 start time matters too. A job that needs to finish before midnight should start earlier — 23:00 or 22:00 — and use a test that does not depend on the clock at the moment of the check.
Fix 2: run on the first, and call it a rollover
Many month-end jobs do not have to run on the last day of the month. They have to run once per month, after the month closes. Statements, usage reports and archive folders all belong to that group.
0 1 1 * * /usr/local/bin/month-rollover.sh
That is one line, no date arithmetic, no leap-year edge cases. The script looks at “yesterday” or at “last month” and does the work. It also survives a server that was off at midnight on the 31st, because the first of the month is a full day of opportunity rather than a single minute.
Choose this shape whenever the business meaning allows it. It is the shortest correct answer, and it removes a class of bugs instead of working around them.
Fix 3: a timer with a calendar expression
systemd timers do not solve the problem, but they do make the intent readable. OnCalendar=*-*-01 01:00:00 is the rollover from the previous section with a clearer name. For “last day”, the same daily-plus-check pattern applies inside the unit:
OnCalendar=*-*-28..31 23:59:00
ExecStart=/usr/local/bin/month-end-guard.sh
The guard script owns the test, which is easier to unit-test than a crontab line. If you are moving a schedule between the two systems anyway, the converter shows which parts carry over and which do not.
Make the guard idempotent
Whatever shape you pick, assume the job can run twice. A retry, a manual run, a clock change or a duplicated crontab entry all produce a second execution. Two defences cost almost nothing:
- A lock file.
flock -n /var/lock/month-end.lock /usr/local/bin/month-end.shrefuses to start while a previous run is still going. - A marker per period. The script writes the month it processed and exits early if that month is already done.
Both are worth adding to a job that touches money or deletes files.
Test it before the month ends
A month-end job is hard to test because the interesting dates come rarely. Two cheap checks catch most mistakes:
- Run the condition by hand on a normal day. The test should say “no” and the script should not run. If it runs, the condition is wrong.
- Read the expression back. The last-day-of-month schedule page shows the pattern with its traps, and the parser will name every value your line produces. The first-day-of-month page is the mirror case, useful when you move a job to the rollover shape.
One more habit: keep the job’s output. A month-end job that fails quietly at 23:59 is the worst kind — you find out weeks later. Redirect the output to a log and read it the next morning, and see where cron logs live if you are not sure where to look.