Cron Expression Deep Dive: Fields, Special Characters, Common Patterns & Troubleshooting

Start with Field Count: 5 vs 6/7

The field count tells you which ecosystem the expression belongs to, and the two syntaxes are not interchangeable:

Standard 5-Field Cron

* * * * *
│ │ │ │ │
│ │ │ │ └── Day of week (0-7, both 0 and 7 = Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of month (1-31)
│ └──────── Hour (0-23)
└─────────── Minute (0-59)

Quartz 6/7-Field Cron

* * * * * * *
│ │ │ │ │ │ │
│ │ │ │ │ │ └── Year (optional, 1970-2099)
│ │ │ │ │ └──── Day of week (1-7, 1=Sunday)
│ │ │ │ └────── Month (1-12)
│ │ │ └──────── Day of month (1-31)
│ │ └────────── Hour (0-23)
│ └──────────── Minute (0-59)
└─────────────── Second (0-59)
Field Standard range Quartz range Allowed special chars
Second — 0-59 , - * /
Minute 0-59 0-59 , - * /
Hour 0-23 0-23 , - * /
Day 1-31 1-31 , - * ? L W C
Month 1-12 or JAN-DEC 1-12 or JAN-DEC , - * /
Day of week 0-7 or SUN-SAT 1-7 or SUN-SAT , - * ? L C #
Year — 1970-2099 , - * /

The difference is not merely two extra fields. In standard cron 0 and 7 are both Sunday; in Quartz 1 is Sunday and 7 is Saturday. Standard cron has no seconds, so the extra leading field is treated as the command: paste 0 0 9 * * ? into a crontab and it fires every day at 09:00 trying to run a command literally named ?. The only trace is command not found — the job looks like it never ran, while it actually fired at the wrong time with the wrong command.

Special Characters in One Table

Symbol Meaning Example Quartz only?
* Every legal value in the field * * * * * every minute No
, List of discrete values 0 0 9,12,18 * * ? 9/12/18 daily No
- Inclusive range 0 0 9-17 * * ? hourly 9:00–17:00 No
/ Step, start/step */15 every 15 min; 0/30 at 0 and 30 No
? Unspecified, defer to the other day field 0 0 12 ? * MON Mondays at noon Yes
L Last 0 0 23 L * ? month end; 6L last Friday Yes
W Nearest weekday 15W nearest weekday to the 15th Yes
# Nth weekday 2#1 first Monday; 6#3 third Friday Yes

The Four Easiest Things to Get Wrong

? Is Not "Any"

* means every legal value counts; ? means "I do not specify this field". In Quartz, day-of-month and day-of-week cannot both be concrete, so one must be ?:

0 0 12 ? * MON       // every Monday at noon: day unspecified
0 0 12 1 * ?         // the 1st at noon: weekday unspecified

Writing ? into a standard 5-field cron is rejected. Conversely, putting * in both day fields in Quartz creates a conflict the scheduler cannot resolve.

L, W, LW: Last Day and Nearest Weekday

L means "last day of the month" in the day field, and "last such weekday" in the weekday field:

0 0 23 L * ?         // last day of month at 23:00
0 0 10 ? * 6L        // last Friday of month at 10:00

W is day-field only: the nearest weekday to a date. 15W lands on the 14th (Friday) when the 15th is Saturday, and on the 16th (Monday) when it is Sunday. LW means the last weekday of the month.

Standard cron has none of these. The portable "last day" trick is 0 0 28-31 * * plus a script check for whether tomorrow is the 1st. @monthly is the 1st, not the last day — do not confuse them.

Day + Weekday: OR in Standard Cron, ? in Quartz

When both day-of-month and day-of-week are concrete, standard cron combines them with OR: 0 0 1 * 1 fires on the 1st or every Monday, not only when both hold. The symptom is a job that runs more often than intended. If you truly need "the 1st only when it is a Monday", check inside the script.

Quartz forbids the ambiguity: one of the two fields must be ?, so the meaning is always deterministic.

DST Jumps and February 29

In DST regions, the transition day contains an hour that does not exist or one that repeats:

  • Spring forward: local 02:00 jumps to 03:00, so 02:00–02:59 never happens and a job scheduled there is silently skipped.
  • Fall back: 02:00–02:59 occurs twice, so the same expression may run twice and break logs, de-duplication, and aggregation.

Avoid scheduling critical work between 2:00 and 3:00, or schedule in UTC. Containers default to UTC, so confirm which clock you actually mean.

0 0 0 29 2 * fires only in leap years; for annual work, decide the date in code rather than relying on it.

Common Schedules

Expression Meaning
* * * * * Every minute
*/5 * * * * Every 5 minutes
0 * * * * Every hour on the hour
0 */2 * * * Every 2 hours
0 0 * * * Every day at midnight
0 9 * * * Every day at 9 AM
0 9 * * 1-5 Weekdays at 9 AM
0 22 * * 1-5 Weekdays at 10 PM
0 0 1 * * The 1st of each month
0 0 1 1 * January 1st each year
0 0 * * 0 Every Sunday
0 0 1 */3 * First day of each quarter
0 0 12 ? * MON-FRI Weekdays at noon (Quartz)
0 0 23 L * ? Last day of month at 23:00 (Quartz)
0 0 9 15W * ? Nearest weekday to the 15th at 9:00 (Quartz)
0 0 10 ? * 6#3 Third Friday at 10:00 (Quartz)
0 30 10-14 ? * MON,WED,FRI Mon/Wed/Fri hourly 10:30–14:30 (Quartz)

Troubleshooting: The Job Did Not Run

Validate the Expression, Then the Time Zone and Environment

  1. The expression: check the next fire time in a parser and confirm day/weekday are not misused.
  2. Time zone: Linux cron uses the server's local zone; Quartz uses the JVM zone; Kubernetes CronJob is fixed to UTC. The 09:00 you meant may be 17:00 in a container.
  3. Environment: cron's PATH is often only /usr/bin:/bin; use absolute paths.
  4. Permissions and line endings: the .sh needs chmod +x; a file edited on Windows can throw bad interpreter.
  5. Logs: cron does not print to a terminal, so redirect output explicitly; on Debian/Ubuntu look for CRON in /var/log/syslog.

When the frequency is wrong, check the step syntax first:

*/5 * * * *     # ✅ minutes 0, 5, 10 … 55
0/5 * * * *     # ⚠️ equivalent on some systems, not all
0-59/5 * * * *  # ✅ explicit range with step

Setting the time zone in Quartz:

CronTrigger trigger = TriggerBuilder.newTrigger()
    .withSchedule(CronScheduleBuilder
        .cronSchedule("0 0 9 * * ?")
        .inTimeZone(TimeZone.getTimeZone("Asia/Shanghai")))
    .build();

Multiple Instances: Every Machine Runs the Same Expression

Standard cron has single-host semantics. Deploy several instances and each one fires the job, duplicating work. Two ways to converge:

  • A distributed lock (Redis lock or a unique DB index) lets only one instance win.
  • Move to a coordinator-aware scheduler (Quartz Cluster, xxl-job, or a K8s CronJob with concurrencyPolicy: Forbid).

A lock only lowers the probability of overlap; the job still has to be idempotent, or retries and backfills will write duplicate data anyway.

Which Library Each Platform Uses

Language / platform Common library Notes
Node.js node-cron, cron 5/6 fields; no L/W/#
Python APScheduler, croniter croniter evaluates expressions only, it does not schedule
Java Quartz, spring-task Quartz supports 6/7 fields and L/W/#
Go robfig/cron 5 fields by default; seconds optional
Kubernetes CronJob Standard 5 fields, always UTC

Reproducible Results

Two checks before shipping:

crontab -l                       # list this user's jobs and confirm the entry exists
grep CRON /var/log/syslog        # Debian/Ubuntu: read the cron daemon's execution log

0 9 * * 1-5 means "Monday to Friday at 09:00". Paste it into the cron parser to see the field breakdown and the next fire time instantly. If the parse matches your intent but the log has no record at that timestamp, the cause is usually the environment or permissions rather than the expression.

Frequently Asked Questions

What is the difference between 5, 6 and 7 field cron?

Standard Unix cron has 5 fields (minute hour day-of-month month day-of-week). Quartz, in the Java ecosystem, uses 6 or 7, adding seconds at the front and optionally year at the end. So 0 0 12 * * ? means 12:00:00 daily in Quartz, but fed to a standard cron it parses into nonsense such as minute 0 of hour 12 with a shifted day field. The two syntaxes are not compatible — verify every expression when migrating. The [Cron parser](/cron-parser.html) shows the actual trigger times before you ship.

What does */5 * * * * actually mean?

Run every 5 minutes. */n means *every n units within that field's range*, triggered on field boundaries: */5 in the minute field fires at minutes 0, 5, 10 … 55, not five minutes from whenever the process started. Also note that when both the day-of-month and day-of-week fields hold specific values, the relationship is OR (execute if either matches), not AND — the single most misunderstood rule for newcomers.

My scheduled job never ran — what should I check first?

Check five things in order: (1) time zone — containers default to UTC, so the 09:00 you meant is 17:00; (2) environment variables — cron's environment differs from an interactive shell and PATH is often minimal, so use absolute paths; (3) script permissions and line endings — the .sh needs chmod +x, and files edited on Windows can throw bad interpreter; (4) logging — cron does not print to a terminal, so redirect output to a file explicitly; (5) the expression itself — verify the trigger times with a parser first.

How do I express the last day of the month?

Standard cron has no last-day syntax, so work around it: (1) list the candidate days — 0 0 28-31 * * combined with a script check for whether tomorrow is the 1st; (2) use @monthly, which equals 0 0 1 * * — the *first* day, not the last; (3) switch to a scheduler supporting the L token (Quartz does). Option 1 is recommended for portability across schedulers.

← Back to Blog