# The POSIX Cron Day-of-Month + Day-of-Week OR Trap (0 9 13 * 5 Runs Every Friday)

> Why specifying both Day-of-Month and Day-of-Week in POSIX crontab evaluates as boolean OR (union) instead of AND, and how to guard intersection schedules.

- **Canonical URL**: https://opslint.dev/cron/posix-dom-dow-or-boolean-trap/
- **Category**: `CRON`
- **Interactive Workbench**: [Open in Live Workbench](https://opslint.dev/cron/#expr=0%209%2013%20*%205&tz=UTC)
- **Normative Specifications**: `POSIX.1-2017 (IEEE Std 1003.1) crontab Utility Specification`, `Vixie Cron 4.1 crontab(5) Day Specification`, `systemd.time(7) Calendar Events (AND Semantics)`, `AWS EventBridge Cron Expressions (? Wildcard Requirement)`

## Incident Symptom & Root Cause

**Production Symptom**: An engineer writes 0 9 13 * 5 expecting a job to run only on 'Friday the 13th' (or 0 9 1-7 * 1 expecting 'the first Monday of the month'), but in production the job fires every single Friday of the year PLUS the 13th of every month (or every day from the 1st through the 7th PLUS every Monday).

**Protocol / Engine Root Cause**: Per POSIX.1-2017 crontab specification, when neither Day-of-Month (field 3) nor Day-of-Week (field 5) is a single asterisk (*), the cron daemon matches dates satisfying EITHER field (boolean OR union), whereas minute, hour, and month combine via boolean AND.

## Remediation Diff (Broken vs Verified Fix)

### Broken POSIX Expression (Evaluates DOM OR DOW Union!)

```text
# Intended: First Monday of every month at 09:00 UTC
# Actual behavior: Runs Dec 1,2,3,4,5,6,7 AND Dec 8,15,22,29 (10+ times/month!)
0 9 1-7 * 1 /opt/scripts/monthly-invoice-run.sh

# Intended: Only Friday the 13th at 09:00 UTC
# Actual behavior: Runs every Friday AND the 13th of every month!
0 9 13 * 5 /opt/scripts/audit-check.sh
```

### Verified POSIX Expression + Shell Intersection Guard

```text
# Fix 1: Keep '*' in Day-of-Week and guard weekday in shell (note % in crontab!)
0 9 1-7 * * [ "$(date +%u)" = "1" ] && /opt/scripts/monthly-invoice-run.sh

# Fix 2: Keep '*' in Day-of-Month and guard date == 13 in shell
0 9 * * 5 [ "$(date +%d)" = "13" ] && /opt/scripts/audit-check.sh

# In GitHub Actions / Kubernetes YAML (no crontab % escaping needed):
# command: ["/bin/sh", "-c", "[ "$(date +%u)" = "1" ] && /opt/scripts/monthly-invoice-run.sh"]
```

## Engineering Deep Dive

The DOM/DOW union rule is one of the oldest and costliest traps in Unix administration. POSIX.1-2017 states explicitly: if either the month or day of month is specified as an element or list, and the day of week is also specified as an element or list, then any day matching either the month and day of month, or the month and day of week, shall be matched. GitHub Actions on.schedule and Kubernetes CronJob inherit this exact POSIX union rule.

Subtle parser quirks make naive workarounds even more dangerous. In Paul Vixie's cron implementation, the OR check literally tests whether the first character of the DOM or DOW string is '*' — meaning an obfuscated wildcard such as */1 or 1-31 in field 3 causes Vixie Cron to treat field 3 as restricted, turning 0 9 */1 * 1 into 'every single day OR Monday' (running 7 days a week!).

Cross-platform migrations fail in the opposite direction as well: systemd OnCalendar expressions such as Fri *-*-13 09:00:00 evaluate weekday and calendar day using boolean AND (intersection), while AWS EventBridge rejects expressions where both fields are specified without a ? wildcard (ValidationException). Whenever you need true intersection in POSIX cron, leave one of field 3 or field 5 as a literal * and enforce the second condition with date +%u or date +%d.

## Day-of-Month + Day-of-Week Boolean Logic Across Platforms

| Case / Engine | Protocol Behavior | Production Remediation |
| :--- | :--- | :--- |
| **POSIX / Vixie Cron / Cronie** (`#posix-vixie-or-union`) | Boolean OR (union) whenever both field 3 (DOM) and field 5 (DOW) are non-'*'. | Set one field to literal '*' and add [ "$(date +\%u)" = "..." ] && guard before command. |
| **GitHub Actions & Kubernetes CronJob** (`#github-actions-k8s-or-union`) | Inherits POSIX boolean OR union; 0 9 1-7 * 1 runs 10–11 times per month. | Use schedule: '0 9 1-7 * *' and check date +%u in step if: or container shell entrypoint. |
| **AWS EventBridge (Rules & Scheduler)** (`#aws-eventbridge-mutual-exclusion`) | Hard syntax error unless either DOM or DOW is '?' (supports native 2#1 Nth-weekday instead). | Use cron(0 9 ? * 2#1 *) for first Monday, or create two separate schedules if union was intended. |
| **systemd.timer (OnCalendar=)** (`#systemd-oncalendar-and-intersection`) | Boolean AND (intersection): Mon *-*-01..07 09:00:00 natively matches ONLY the first Monday. | When migrating systemd timers back to crontab, NEVER copy DOM+DOW directly without a shell guard. |

## Frequently Asked Engineering Questions

### Why did my crontab guard [ "$(date +%u)" = "1" ] fail with an unexpected EOF error?

In standard Vixie /etc/crontab and user crontabs, unescaped percent signs (%) are converted into newline characters by the cron daemon before passing the command to /bin/sh, truncating the command at +%u. Always escape percent signs as \% inside crontab files (do NOT escape % inside Kubernetes YAML or GitHub Actions run: blocks).

### Does date +%u use 0 or 7 for Sunday?

ISO 8601 date +%u always maps Monday=1 through Sunday=7 across GNU coreutils, BSD/macOS, and BusyBox. By contrast, date +%w maps Sunday=0 through Saturday=6. Prefer +%u so Monday is unambiguously 1.

### Which field should keep '*' when guarding Nth weekday of the month?

Leave Day-of-Week as '*' (0 9 1-7 * *) so cron wakes up only 7 mornings per month instead of waking up every Monday (4–5 mornings) or vice versa—waking on the 4–5 target weekdays (0 9 * * 1) with a [ "$(date +\%d)" -le 7 ] check actually runs slightly fewer shell evaluations (4.3 vs 7 per month).

## Related Problem-Cluster Guides

- [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.
- [Fix Crontab Percent Sign (%) Syntax Errors and Missing EOF Newline Silent Skips](https://opslint.dev/cron/crontab-percent-sign-newline-eof-pitfalls/) — Why date +%Y-%m-%d crashes inside Linux crontab with unexpected EOF while succeeding in bash, and why missing trailing newlines drop the last cron job.
- [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.
- [Fix AWS EventBridge Cron ValidationException: The ? vs * Wildcard Rule](https://opslint.dev/cron/aws-eventbridge-question-mark-wildcard-error/) — Resolve AWS EventBridge Parameter ScheduleExpression is not valid errors caused by 5-field POSIX syntax or using * in both Day-of-Month and Day-of-Week.
