Nearest Weekday Payroll Cron Schedules: 15W and LW vs Portable POSIX Guards
Translate AWS EventBridge 15W and LW nearest-business-day payroll modifiers to POSIX crontab, Kubernetes CronJob, and GitHub Actions without month-crossing bugs.
Incident Symptom & Protocol Root Cause
Production Failure Symptom
Semi-monthly payroll or ACH settlement jobs migrated from AWS EventBridge cron(0 9 15W * ? *) fail syntax validation in Kubernetes or Linux crontab, or naive Monday fallback logic shifts the 1st of the month backward into the previous fiscal month.
Scheduler Parser & Clock Mechanics
POSIX cron lacks the Quartz 'W' (nearest weekday within the same calendar month) modifier. True 'W' semantics shift Saturday to Friday (day-1) EXCEPT on the 1st of the month (where 1W on Saturday shifts forward to Monday the 3rd so execution never crosses a month boundary), and shift Sunday forward to Monday (day+1) EXCEPT on the last day of the month.
Broken vs Verified Configuration
# Fails syntax check in POSIX crontab, GitHub Actions, and Kubernetes:
0 9 15W * * /opt/payroll/ach-submit.sh
# Broken workaround: runs on 14, 15, 16 AND every Mon-Fri all month!
0 9 14-16 * 1-5 /opt/payroll/ach-submit.sh# 1. Portable POSIX 15W (14th if Fri, 15th if Mon-Fri, 16th if Mon):
# Wakes only on days 14, 15, 16; fires once on the exact nearest weekday!
0 9 14-16 * * d=$(date +%d); u=$(date +%u); ( [ "$d" = "14" ] && [ "$u" = "5" ] ) || ( [ "$d" = "15" ] && [ "$u" -le 5 ] ) || ( [ "$d" = "16" ] && [ "$u" = "1" ] ) && /opt/payroll/ach-submit.sh
# 2. Boundary-safe 1W (1st if Mon-Fri, 2nd if Mon, 3rd if Mon — never previous month!):
0 9 1-3 * * d=$(date +%d); u=$(date +%u); ( [ "$d" = "01" ] && [ "$u" -le 5 ] ) || ( [ "$d" = "02" ] && [ "$u" = "1" ] ) || ( [ "$d" = "03" ] && [ "$u" = "1" ] ) && /opt/payroll/ach-submit.sh
# 3. Native AWS EventBridge Scheduler (IANA timezone + 15W):
# cron(0 9 15W * ? *)Operational Deep Dive
Understanding how the Quartz and AWS EventBridge W character resolves weekend collisions is essential before porting banking or payroll jobs. For any mid-month target date N (between 2 and 27), if day N falls on Monday through Friday, it fires on day N; if day N falls on Saturday, it shifts one day earlier to Friday (N-1); if day N falls on Sunday, it shifts one day later to Monday (N+1). Consequently, 15W only ever fires on the 14th (when the 14th is Friday), the 15th (when the 15th is Monday–Friday), or the 16th (when the 16th is Monday).
Month boundaries add a non-negotiable fiscal constraint: W NEVER crosses into an adjacent month! When 1W falls on Saturday the 1st, shifting backward to Friday would land in the prior accounting month, so 1W shifts forward two days to Monday the 3rd. Symmetrically, LW (last weekday of the month) shifts Saturday or Sunday backward to Friday so month-end payroll settles inside the closing month.
Note that cron engines have zero awareness of national banking holidays (such as US Federal Reserve ACH holidays on Mondays like Labor Day or Memorial Day). Any real banking or ACH submission job should still check an explicit holiday calendar table inside application code in addition to weekday cron scheduling.
Nearest-Weekday ('W') Resolution Truth Table
| Engine / Case | Runtime Behavior | Production Remediation |
|---|---|---|
| Mid-month 15W | Saturday 15th -> Friday 14th; Sunday 15th -> Monday 16th; Weekday 15th -> 15th. | Restrict cron DOM to 14-16 with '*' in DOW and test (14+Fri) || (15+Mon..Fri) || (16+Mon). |
| Start-of-month 1W | Saturday 1st -> Monday 3rd (never previous month!); Sunday 1st -> Monday 2nd. | Restrict cron DOM to 1-3 and test (1+Mon..Fri) || (2+Mon) || (3+Mon). |
| Last weekday of month (LW) | Last calendar day if Mon–Fri; shifts back 1–2 days to Friday if month ends on Sat/Sun. | EventBridge: cron(0 9 LW * ? *) | POSIX: run 26-31 and check last business day in script. |
| Combining W with lists/ranges (1,15W) | EventBridge rejects comma lists with W in a single expression (ValidationException). | Provision two separate schedules: cron(0 9 1W * ? *) and cron(0 9 15W * ? *). |
Edge Cases & Engineering FAQs
Can I write cron(0 9 1W,15W * ? *) in a single AWS EventBridge schedule?
No. In Quartz and AWS EventBridge, the W modifier applies to a single day-of-month value and cannot be combined with comma lists or ranges. Create one schedule for 1W and a second schedule for 15W targeting the same function.
Does systemd OnCalendar have a built-in 15W equivalent?
No. systemd.time(7) supports Mon..Fri intersection and ~ reverse month-end offsets, so 'last Friday or weekday' still requires either a wrapper condition or splitting into explicit calendar expressions.
Does 15W account for Federal Reserve or NYSE market holidays?
Never. Cron parsers only inspect the Gregorian calendar weekday (Monday through Sunday). Always validate against an explicit holiday list inside the job binary when initiating ACH wires or equity orders.
Runtime Dead-Man's-Switch Verification
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.