Understanding Cron Expressions: The Complete Guide to Scheduling
A developer's guide to mastering cron syntax, scheduling automated tasks, and avoiding common timing mistakes.
If you've ever had to schedule a recurring task—whether it's sending an email newsletter, backing up a database, or clearing cache—you've likely encountered a cron expression. They often look like a random assortment of stars and numbers (e.g., 0 15 * * *), but once you understand the pattern, they are incredibly powerful.
What is a Cron Expression?
A standard cron expression is a string comprising five (or sometimes six) fields separated by spaces. These fields represent a set of times or dates when a task should execute. Originally developed for Unix-like operating systems, the cron syntax is now ubiquitous across cloud providers, CI/CD pipelines, and application frameworks.
* * * * * *
- - - - - -
| | | | | |
| | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
| | | | +---------- month (1 - 12)
| | | +--------------- day of month (1 - 31)
| | +-------------------- hour (0 - 23)
| +------------------------- minute (0 - 59)
+------------------------------ second (0 - 59, optional in 6-field format)Special Characters Explained
The real power of cron comes from its special characters:
- Asterisk (
*): Means "every". A*in the hour field means "every hour". - Comma (
,): Specifies a list of values.1,15in the minutes field means "at minute 1 and minute 15". - Hyphen (
-): Defines a range.1-5in the day-of-week field means "Monday through Friday". - Slash (
/): Specifies increments.*/15in the minutes field means "every 15 minutes".
Common Examples
| Expression | Meaning |
|---|---|
| 0 * * * * | Every hour, at the start of the hour |
| 0 0 * * * | Every day at midnight |
| 0 9 * * 1-5 | At 9:00 AM, Monday through Friday |
| */15 * * * * | Every 15 minutes |
Generating and Validating Cron Expressions
Writing cron expressions by hand is notoriously prone to errors. A simple mistake—like confusing the day-of-month and day-of-week fields—can cause your backup script to run every minute instead of once a day!
To eliminate guesswork, use our Cron Expression Generator. It allows you to type natural language like "Every day at 3pm" and instantly get the correct 0 15 * * * expression. It even supports 6-field Quartz syntax (which includes seconds).
Part of the ThenCatch blog. Learn more about us or browse more guides.