Cron Syntax,
by Example.
Cron is five numbers and four symbols, and many people learn it by copying a line that looked close enough. Two of its rules are counterintuitive, and both of them silently run your job at the wrong time with no error.
The Five Fields
* * * * *
| | | | |
| | | | +-- day of week 0-7, 0 and 7 both Sunday
| | | +----- month 1-12
| | +-------- day of month 1-31
| +----------- hour 0-23
+-------------- minute 0-59
A sixth field for seconds exists in Quartz and in some schedulers, and is not standard cron. If a line has six fields, find out which flavor you are in before trusting it.
The Day of Month and Day of Week Trap
If both day fields are restricted, cron runs when either matches. Cron treats the two day fields as OR.
0 0 13 * 5 every 13th, AND every Friday.
Not "Friday the 13th".
That line fires on the 13th of every month and on every Friday, which is about sixty times a year. To get a real AND you restrict one field in cron and check the other inside the job.
If only one of the two is restricted the behavior is the obvious one, which is why the trap stays hidden until the first time someone uses both.
Steps, and What They Count From
*/15 in the minute field means every 15 steps from the start of the range, so 0, 15, 30, and 45. The time you saved the crontab plays no part.
The consequence shows up when the step does not divide the range evenly. */45 gives 0 and 45, then the hour restarts and it fires at 0 again. The gap is 45 minutes, then 15. If you want a 45 minute cycle, cron is the wrong tool.
| Expression | Fires |
|---|---|
| */15 * * * * | :00, :15, :30, :45 |
| 0 */3 * * * | Midnight, 3am, 6am, and so on |
| 0 9-17 * * 1-5 | Every hour, 9am to 5pm, weekdays |
| 30 2 1 * * | 2:30am on the first of the month |
| 0 0 * * 0 | Midnight on Sunday |
Timezones and the Twice Yearly Bug
Cron uses the machine timezone, which is usually UTC on a server and local time on a laptop. That difference alone accounts for a lot of jobs that appear to run at the wrong hour.
Worse, if the machine runs local time with daylight saving, the clock jumps. A job scheduled at 2:30am does not run at all on the spring forward day, because 2:30 never happens, and runs twice on the fall back day. Avoid scheduling anything financial or idempotency sensitive between 1am and 3am local time.
Run servers in UTC and convert in your application. It removes the whole class of problem.
The Shorthands
@daily, @hourly, @weekly, @monthly, @yearly, and @reboot are supported by most implementations. They are clearer than the equivalent numbers and worth using where they fit. @reboot is the odd one out: it runs once at startup and follows no schedule.
Things That Are Not Standard Cron
Lfor last day of month. Quartz only.Wfor nearest weekday. Quartz only.#for nth weekday, as in6#3. Quartz only.- Seconds as a sixth field. Quartz, AWS, and others.
?as a placeholder in a day field. Quartz and AWS.
Copying one of those into a standard crontab produces a parse error at best, and a silently unscheduled job at worst.