Cron Expressions Made Simple
Cron is the scheduler built into Linux and many hosting platforms. A cron expression describes when a task should run, using five fields separated by spaces.
The five fields
minute hour day-of-month month day-of-week
0-59 0-23 1-31 1-12 0-6 (Sunday = 0)
Special characters
*means "every" value.,separates a list:1,15means the 1st and 15th.-is a range:1-5means Monday to Friday in the day-of-week field./is a step:*/15means every 15 units.
Examples
0 * * * *— at the start of every hour.*/15 * * * *— every 15 minutes.30 2 * * *— every day at 02:30.0 9 * * 1-5— at 09:00 on weekdays.0 0 1 * *— at midnight on the first day of each month.
Check before you deploy
A schedule that runs an hour late, or every minute instead of every hour, can cause real problems. Build your schedule with the Cron Expression Builder, then paste an existing one into the Cron Expression Parser to see it described in plain language.
Time zone gotchas
Cron runs in the server's time zone, which is often UTC. If you want something to run at 09:00 local time, convert it with the Time Zone Converter first, and remember daylight-saving changes.
Good habits
- Make jobs safe to run twice, in case one run overlaps the next.
- Log output so you can see failures.
- Avoid scheduling many heavy jobs at exactly midnight; stagger them by a few minutes.
Frequently asked questions
Does day-of-week 7 mean Sunday?
In many implementations both 0 and 7 mean Sunday, but not all. Using 0 is the safest.
What if both day-of-month and day-of-week are set?
In classic cron, the job runs when either field matches, which surprises many people. Set only one of them unless you intend that behaviour.
Can cron run something every second?
Standard cron works to the minute. For finer schedules use a different scheduler or a loop inside your program.
Why did my job not run?
Typical causes are a wrong time zone, a missing environment (cron has a minimal PATH), file permissions, or errors that went unlogged. Redirect output to a log file while you debug.
Key takeaways
- Five fields: minute, hour, day of month, month, day of week.
- Test your expression in a parser.
- Log the output and mind the server time zone.