Run a Cron Job on the Last Day of Every Month (28, 29, 30, and 31 Leap-Year Safe)

Portable month-end cron patterns across POSIX Linux/BSD, Kubernetes CronJob, GitHub Actions, AWS EventBridge (L), and systemd (~01) including February 29.

POSIX.1-2017 crontab (No Native 'L' Modifier)AWS EventBridge Cron 'L' Last-Day-of-Month Modifiersystemd.time(7) Reverse Day-of-Month Offset (*-*~01)

Incident Symptom & Protocol Root Cause

Production Failure Symptom

Month-end billing close or financial snapshot jobs written with 55 23 31 * * skip April, June, September, November, and February completely—or three-line month splits (31 for 1,3,5... and 28 for Feb) run one day early on February 28 during leap years such as 2028.

Scheduler Parser & Clock Mechanics

Standard POSIX crontab lacks a dynamic month-length token like Quartz/EventBridge's L modifier, and hardcoding 28 2 * fails every leap year when February has 29 days.

Broken vs Verified Configuration

BrokenBroken Month-End Crontab (Skips 30-Day Months or Fails Leap Years)
# Trap A: 'L' crashes standard crontab, GitHub Actions, and Kubernetes CronJob!
55 23 L * * /opt/billing/close-month.sh

# Trap B: Static 3-rule split fires a day early on Feb 28 in leap years (2028, 2032)!
55 23 31 1,3,5,7,8,10,12 * /opt/billing/close-month.sh
55 23 30 4,6,9,11 *        /opt/billing/close-month.sh
55 23 28 2 *               /opt/billing/close-month.sh
Verified FixLeap-Year-Exact Last Day of Month Across Engines
# 1. Linux (GNU coreutils date) & BusyBox date (-d tomorrow / -D):
# Wakes only on days 28..31; exits 0 cleanly on non-last days, runs on exact last day!
55 23 28-31 * * [ "$(date -d tomorrow +%d)" != "01" ] || /opt/billing/close-month.sh

# 2. BSD / macOS date (-v+1d):
55 23 28-31 * * [ "$(date -v+1d +%d)" != "01" ] || /opt/billing/close-month.sh

# 3. Zero-dependency Python/POSIX portable guard (works identically on GNU, BSD, Alpine):
# python3 -c 'import datetime as d,sys; sys.exit(0 if (d.date.today()+d.timedelta(1)).day==1 else 1)'

# 4. AWS EventBridge Scheduler (native 'L'):
# cron(55 23 L * ? *)

# 5. systemd.timer (native '~01' reverse offset):
# OnCalendar=*-*-~01 23:55:00

Operational Deep Dive

Checking whether 'tomorrow is day 01' delegates all Gregorian calendar and leap-year rules (divisible by 4, except centuries not divisible by 400) directly to libc/tzdata. By restricting cron field 3 to 28-31, the cron daemon only spawns the shell guard four times per month at most—and on the true final day (Feb 28 in common years, Feb 29 in leap years, day 30 in April/June/Sept/Nov, day 31 otherwise), tomorrow's date +%d evaluates to 01.

Pay close attention to three subtle production edge cases when running the tomorrow check! First, notice the exit-code idiom [ "$(date -d tomorrow +\%d)" != "01" ] || /opt/billing/close-month.sh: on February 28 of a leap year, the != "01" test succeeds (exit 0) and short-circuits before running close-month.sh, so Kubernetes CronJobs and systemd wrappers see a clean exit code 0 instead of an artificial Pod failure! Second, GNU date -d tomorrow adds 24 hours or 1 calendar day relative to local time—if your job runs at 23:55 on a DST transition night, prefer date -d '+1 day' at noon or anchor TZ=UTC for date math: [ "$(TZ=UTC date -d tomorrow +\%d)" != "01" ] when your CronJob runs in UTC.

Third, verify which date binary exists inside slim container images! Debian/Ubuntu slim images ship GNU coreutils (date -d tomorrow), macOS/FreeBSD ship BSD date (date -v+1d), while Alpine Linux BusyBox date supports date -d @$(($(date +%s) + 86400)) only if run well away from 23:00+ DST shifts—or even simpler, ask whether running at 00:00 on the 1st of the new month (0 0 1 * *) with a --period=previous-month flag eliminates the shell guard altogether!

Last-Day-of-Month Implementation by Environment

Engine / CaseRuntime BehaviorProduction Remediation
Debian / Ubuntu / RHEL (GNU date)Supports date -d tomorrow +%d natively in coreutils.55 23 28-31 * * [ "$(date -d tomorrow +\%d)" != "01" ] || /path/to/job
FreeBSD / OpenBSD / macOSGNU -d flag is invalid; uses -v+1d calendar adjustment.55 23 28-31 * * [ "$(date -v+1d +\%d)" != "01" ] || /path/to/job
AWS EventBridge (Rules & Scheduler)Native Quartz 'L' token in Day-of-Month field (requires '?' in DOW).cron(55 23 L * ? *)
systemd.timer (v233+)Tilde '~' counts backward from end of month (~01 = last day, ~03 = 3rd-to-last day).OnCalendar=*-*-~01 23:55:00
00:05 on Day 1 (Architectural Simplification)0 0 1 * * works natively in 100% of cron dialects with zero shell wrappers.Run at 00:05 on the 1st and query closed window [< first_of_current_month] in SQL.

Edge Cases & Engineering FAQs

Why is running month-end billing at 00:05 on the 1st often safer than 23:55 on day L?

Running at 23:55 on the last day leaves a 5-minute blind spot (23:55:00–23:59:59) where late transactions still belong to the closing month! Running 5 0 1 * * after midnight guarantees 100% of the prior month's UTC timestamps have closed and works natively in every cron engine without shell guards.

Does Cloudflare Workers Cron Triggers support the 'L' last-day-of-month token?

Yes! Cloudflare Workers Cron Triggers support L in the Day-of-month and Day-of-week fields (for example 55 23 L * * in UTC).

How do I schedule 2 days before the end of the month?

In systemd, use OnCalendar=*-*-~03 09:00:00 (~01 is last day, ~03 is 2 days prior). In POSIX cron with GNU date, use 0 9 26-29 * * [ "$(date -d '+3 days' +\%d)" != "01" ] || cmd.

Runtime Dead-Man's-Switch Verification

Heartbeat & Cron MonitoringBetter Stack →

Dead-man's-switch heartbeat checks that page on-call when a scheduled job skips a DST gap, stalls on a lock, or exits non-zero.