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

- **Canonical URL**: https://opslint.dev/cron/last-day-of-month-leap-year-cron-patterns/
- **Category**: `CRON`
- **Interactive Workbench**: [Open in Live Workbench](https://opslint.dev/cron/#expr=55%2023%2028-31%20*%20*&tz=UTC)
- **Normative Specifications**: `POSIX.1-2017 crontab (No Native 'L' Modifier)`, `AWS EventBridge Cron 'L' Last-Day-of-Month Modifier`, `systemd.time(7) Reverse Day-of-Month Offset (*-*~01)`

## Incident Symptom & Root Cause

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

**Protocol / Engine Root Cause**: 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.

## Remediation Diff (Broken vs Verified Fix)

### Broken Month-End Crontab (Skips 30-Day Months or Fails Leap Years)

```text
# 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
```

### Leap-Year-Exact Last Day of Month Across Engines

```text
# 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
```

## Engineering 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

| Case / Engine | Protocol Behavior | Production Remediation |
| :--- | :--- | :--- |
| **Debian / Ubuntu / RHEL (GNU date)** (`#last-day-gnu-linux`) | Supports date -d tomorrow +%d natively in coreutils. | 55 23 28-31 * * [ "$(date -d tomorrow +\%d)" != "01" ] \|\| /path/to/job |
| **FreeBSD / OpenBSD / macOS** (`#last-day-bsd-macos`) | GNU -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)** (`#last-day-aws-eventbridge`) | Native Quartz 'L' token in Day-of-Month field (requires '?' in DOW). | cron(55 23 L * ? *) |
| **systemd.timer (v233+)** (`#last-day-systemd-tilde`) | 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)** (`#first-of-next-month-alternative`) | 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. |

## Frequently Asked Engineering Questions

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

## Related Problem-Cluster Guides

- [Quarterly Cron Schedules: Calendar Quarter Starts vs Fiscal Quarter-End Closes](https://opslint.dev/cron/quarterly-and-fiscal-year-boundary-schedules/) — Avoid off-by-one month bugs in */3 quarterly cron expressions, align custom fiscal quarters (Feb/May/Aug/Nov), and handle uneven 30/31-day quarter-end closes.
- [Schedule the First Monday or Third Tuesday of Every Month in Portable Cron](https://opslint.dev/cron/nth-weekday-of-month-portable-cron-guard/) — How to run jobs on the Nth weekday of the month across POSIX crontab, GitHub Actions, Kubernetes CronJob, AWS EventBridge (#), and systemd timers.
- [Nearest Weekday Payroll Cron Schedules: 15W and LW vs Portable POSIX Guards](https://opslint.dev/cron/nearest-weekday-payroll-schedule-15w/) — Translate AWS EventBridge 15W and LW nearest-business-day payroll modifiers to POSIX crontab, Kubernetes CronJob, and GitHub Actions without month-crossing bugs.
- [systemd OnCalendar vs POSIX Cron: AND Logic, Reverse Offsets (~), and Persistent=true](https://opslint.dev/cron/systemd-oncalendar-to-cron-and-logic-differences/) — Avoid critical translation bugs between systemd.timer OnCalendar expressions and 5-field POSIX cron, including DOM+DOW intersection, seconds, and missed boot catch-up.
