Schedule the First Monday or Third Tuesday of Every Month in Portable Cron
How to run jobs on the Nth weekday of the month across POSIX crontab, GitHub Actions, Kubernetes CronJob, AWS EventBridge (#), and systemd timers.
Incident Symptom & Protocol Root Cause
Production Failure Symptom
Attempting to use 0 9 ? * 2#1 or 0 9 * * 1#1 in Linux crontab, Kubernetes CronJob, or GitHub Actions fails with bad day-of-week syntax errors—while changing it to 0 9 1-7 * 1 triggers the POSIX OR trap and runs 10 times a month.
Scheduler Parser & Clock Mechanics
Standard 5-field POSIX crontab and robfig/cron (without optional non-standard Quartz flags) do not implement the # Nth-occurrence operator, and combining day-of-month ranges (1-7, 8-14, 15-21, 22-28) with a day-of-week number evaluates as boolean OR instead of AND.
Broken vs Verified Configuration
# Mistake 1: Quartz/EventBridge '#' operator rejected by POSIX crontab & GitHub Actions
0 9 * * 1#1 /usr/local/bin/monthly-patching.sh
# Mistake 2: 0 9 15-21 * 2 runs EVERY day 15..21 PLUS EVERY Tuesday all month!
0 9 15-21 * 2 /usr/local/bin/patch-tuesday-rollout.sh# 1. Linux /etc/crontab or crontab -e (escape % as %!):
# First Monday (days 1-7 AND ISO weekday 1):
0 9 1-7 * * [ "$(date +%u)" = "1" ] && /usr/local/bin/monthly-patching.sh
# Third Tuesday (days 15-21 AND ISO weekday 2):
0 9 15-21 * * [ "$(date +%u)" = "2" ] && /usr/local/bin/patch-tuesday-rollout.sh
# 2. GitHub Actions YAML (no % escaping needed inside run:):
# schedule: [{ cron: "0 9 * * 2", timezone: "America/New_York" }]
# steps:
# - if: steps.check.outputs.run == 'true'
# - run: '[ "$(date +%d)" -ge 15 ] && [ "$(date +%d)" -le 21 ] && ./rollout.sh'
# 3. AWS EventBridge Scheduler (native '#', 1=SUN, 2=MON, 3=TUE):
# cron(0 9 ? * 3#3 *)
# 4. systemd.timer (native AND intersection):
# OnCalendar=Tue *-*-15..21 09:00:00Operational Deep Dive
Every Nth occurrence of a weekday falls inside a deterministic 7-day calendar slice of the month: the 1st occurrence is always between day 01 and 07; the 2nd occurrence is always between 08 and 14; the 3rd occurrence is always between 15 and 21; and the 4th occurrence is always between 22 and 28. Because every month has at least 28 days, those four 7-day windows each contain every weekday (Monday through Sunday) once and only once!
To convert that mathematical guarantee into a bug-free POSIX schedule, restrict ONLY one cron field and check the other inside /bin/sh. You can either wake up on the 7 candidate dates (0 9 15-21 * *) and verify [ "$(date +\%u)" = "2" ], OR wake up only on the target weekday (0 9 * * 2, which runs 4–5 times per month) and test the day-of-month window ([ "$(date +\%d)" -ge 15 ] && [ "$(date +\%d)" -le 21 ] -> note leading-zero octal safety in POSIX test: use $((10#$(date +\%d))) in bash or date +\%-d in GNU date so '08' and '09' are never parsed as invalid octal!).
Because 08 and 09 can trigger octal arithmetic errors in some shells when compared numerically, triggering on the calendar range inside cron (0 9 8-14 * *) and comparing ISO weekday as a single-digit string ([ "$(date +\%u)" = "2" ]) is completely immune to leading-zero octal bugs across dash, bash, zsh, and BusyBox ash!
Nth Weekday of Month Calendar Slices & Guards
| Engine / Case | Runtime Behavior | Production Remediation |
|---|---|---|
| 1st Weekday (e.g. 1st Monday) | Falls strictly inside calendar days 1–7 of every month. | POSIX: 0 9 1-7 * * [ "$(date +\%u)" = "1" ] && cmd | EventBridge: cron(0 9 ? * 2#1 *) |
| 2nd Weekday (e.g. Patch Tuesday) | Falls strictly inside calendar days 8–14 (avoid shell numeric -ge 08 octal trap!). | POSIX: 0 9 8-14 * * [ "$(date +\%u)" = "2" ] && cmd | EventBridge: cron(0 9 ? * 3#2 *) |
| 3rd Weekday (e.g. 3rd Friday options expiry) | Falls strictly inside calendar days 15–21. | POSIX: 0 9 15-21 * * [ "$(date +\%u)" = "5" ] && cmd | EventBridge: cron(0 9 ? * 6#3 *) |
| 4th Weekday (e.g. US Thanksgiving 4th Thu in Nov) | Falls strictly inside calendar days 22–28. | POSIX: 0 9 22-28 11 * [ "$(date +\%u)" = "4" ] && cmd | EventBridge: cron(0 9 ? 11 5#4 *) |
| Last Friday of Month (4th or 5th!) | Cannot use 22-28 because 30/31-day months often have a 5th Friday on days 29–31. | Run every Friday (0 9 * * 5) and test [ "$(date -d '+7 days' +\%m)" != "$(date +\%m)" ] && cmd. |
Edge Cases & Engineering FAQs
What happens if a shell guard exits 1 when today is NOT the target weekday?
If you write [ "$(date +\%u)" = "1" ] && my_cmd, when the test is false the compound command returns exit status 1! In Kubernetes CronJobs or monitoring wrappers that treat non-zero exit codes as Pod failures, invert the guard with an OR early exit: [ "$(date +%u)" != "1" ] || my_cmd so non-matching days exit 0 cleanly!
How do I schedule the LAST Monday of the month when some months have 4 Mondays and others have 5?
Schedule on every Monday (0 9 * * 1) and verify that adding 7 days lands in the next month: [ "$(date -d '+7 days' +\%d)" -le 7 ] (or compare +%m). In systemd, use OnCalendar=Mon *-*-~01..07 09:00:00 (reverse-offset tilde syntax).
Why did [ "$(date +%d)" -eq 08 ] fail in /bin/sh or bash arithmetic?
date +%d zero-pads single-digit days ('08', '09'). In C/bash arithmetic contexts (( 08 )), a leading 0 denotes base-8 octal where digits 8 and 9 are illegal. Matching ISO weekday string +%u ('1'..'7') avoids zero-padding completely.
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.