A cron expression is the terse five-field incantation behind every scheduled job that runs at 3 a.m. while everyone sleeps. It tells the cron daemon exactly when to fire a task, and its compactness is both the point and the danger: one mistyped character and your backup runs hourly instead of daily. This guide decodes all five fields, the special characters, and the edge cases that make experienced engineers triple-check their expressions.
The five fields
A standard cron expression is five whitespace-separated fields, from most specific to least specific:
| Position | Field | Allowed values |
|---|---|---|
| 1 | minute | 0-59 |
| 2 | hour | 0-23 |
| 3 | day of month | 1-31 |
| 4 | month | 1-12 or JAN-DEC |
| 5 | day of week | 0-7 or SUN-SAT (0 and 7 both mean Sunday) |
The expression 0 3 * * * means minute 0, hour 3, any day-of-month, any month, any day-of-week — in other words, every day at 03:00.
Special characters
- * — any value. 0 * * * * runs at minute 0 of every hour.
- , — lists. 0 9,17 * * * runs at 09:00 and 17:00 every day.
- - — ranges. 0 9-17 * * * runs once an hour between 09:00 and 17:00 inclusive.
- / — steps. */15 * * * * walks the allowed set every 15 minutes; 0 0-6/2 * * * steps by two over the hour range 0-6.
- L — the last. 0 0 L * * fires on the last day of the month; 0 0 * * 5L means the last Friday.
- W — nearest weekday. 0 0 15W * * runs on the weekday nearest the 15th.
- # — nth occurrence. 0 0 * * 1#2 runs on the second Monday of the month (day 1, occurrence 2).
- ? — "no particular value," used only in the day fields of tools such as Quartz that use a six-field dialect.
Real-world expressions
| Expression | Meaning |
|---|---|
| 0 0 * * * | Every day at midnight |
| 30 2 * * 1 | Every Monday at 02:30 |
| 15 14 1 * * | At 14:15 on the first day of the month |
| 0 22 * * 1-5 | Weeknights at 22:00 |
| */10 * * * * | Every 10 minutes |
| 0 0 1 */3 * | Quarterly: midnight on the 1st of every 3rd month |
| 45 9 1,15 * * | 09:45 on the 1st and the 15th of each month |
The gotchas that bite in production
Cron runs on the server's local time, not your clock. If the machine is UTC and you scheduled 0 9 * * * expecting 09:00 local, the job fires at 09:00 UTC instead. In the same way, a step through wall-clock time misbehaves across daylight-saving transitions: a job at 2:30 in a region that springs forward never runs that hour, and in autumn it can run twice in the same day. The only reliable answer is to run cron itself in UTC and let your application convert.
Common bugs to avoid
- Hour 24. Valid hours are 0-23 — 24 is silently rejected or ignored, and 0 is the correct midnight value.
- Minute 60. The five-field dialect has no seconds field; 60 in the minute position is invalid. For second-level schedules you need a six-field dialect.
- Mixing day-of-month and day-of-week. When both fields are restricted, cron runs when either matches. There is no AND between them — a famous surprise for schedules written with both.
- Assuming 0 = Monday. Several systems number Sunday as 0, which is exactly why 0 and 7 both mean Sunday in the standard.
Generating, testing and wiring schedules
Never write the first day-of-week math in your head. The Cron Expression Generator builds valid expressions field by field and shows the next five fire times, which kills 90% of syntax bugs before they reach the server. If your scheduler loads expressions from external configuration, validate the parser side as well — the Regex Tester is the right place to prototype and debug the pattern that extracts or validates those strings. When job configuration lives in versioned JSON, keep it tidy with the JSON Formatter before code review.
Frequently asked questions
Why is my cron job not running at the right time? Time zones first — the server's local time wins. Then check whether you restricted both day-of-month and day-of-week (an OR, not an AND), and whether hour or minute values are out of the field's valid range.
How do I run a job every five minutes? The step syntax */5 * * * * runs every 5 minutes of every hour. For five minutes on the hour multiples only, write 0,5,10,15,20,25,30,35,40,45,50,55 * * * * instead.
Can cron run a job on the last day of the month? Yes, with the L character: 0 9 L * * runs at 09:00 on the last day of every month, automatically handling 28, 29, 30 and 31. Standard cron supports this in the day-of-month field.