Simple string combinations can produce remarkable results. This holds true for regular expressions and the cron scheduling system alike. Both appear deceptively straightforward, yet improper usage can lead to amusing—or disastrous—outcomes. For instance, I once attempted to schedule a task to run every four hours and wrote the following expresssion:
* */4 * * *
This configuration actually executes every minute for four consecutive hours—in other words, 60 times rather than once. If this task were resource-intensive, the server might have ground to a halt. With that cautionary tale in mind, let's explore cron syntax. Before proceeding, here's a useful resource: crontab.guru, which translates expressions into plain English. Those unfamiliar with cron should use this tool to validate their expressions and avoid similar mistakes.
Structure Overview
It's worth noting that different cron implementations vary slightly—some support seconds, and some allow combined use of commas and hyphens. The following structure follows the standard definition from Wikipedia:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
A standard cron expression comprises five fields: minute, hour, day of the month, month, and day of the week. The distinction between "day of the month" and "day of the week" is important—the former represents the numeric date, while the latter represents the weekday. Each field accepts the following values:
- Minute: 0-59, or *
- Hour: 0-23, or *
- Day of month: 1-31, or *
- Month: 1-12, or *
- Day of week: 0-6, or *
Basic values alone prove insufficient for complex schedules. For example, to run a task every minute, the expression is straightforward:
* * * * *
However, for intervals like "every five minutes," additional operators become necessary.
Intervals — The Forward Slash (/)
The forward slash simply denotes "every X units." Using this operator, a task running every five minutes appears as:
*/5 * * * *
What if execution should only occur during a specific range, such as between minutes 20 and 30? The hyphen operator addresses this scenario.
Ranges — The Hyphen (-)
20-30 * * * * Execute during minutes 20 through 30 of every hour
30 * * * * Execute only at minute 30 of every hour
20-30/2 * * * * Execute every 2 minutes within the 20-30 range
Lists — The Comma (,)
While the hyphen typically represents consecutive values, the comma handles discrete values. For instance, to run a task at minutes 2, 3, 5, 7, and 11:
2,3,5,7,11 * * * *
These operators can be combined for more complex schedules:
2,5-13/4 4 * * * Execute at minute 2, then every 4 minutes from 5 through 13, at 4 AM
2-30/7,5-13/4 * * * * Execute every 7 minutes from 2-30, and every minute from 5-13
With the hyphen, forward slash, and comma operators, nearly any scheduling pattern becomes expressible.
Abbreviated Syntax
Months and weekdays also support textual abbreviations:
5 0 * JAN-JUL MON-THU At 00:05 on every weekday from Monday through Thursday in every month from January through July