Understanding Cron Syntax
Cron is the time-based job scheduler in Unix-like operating systems. It uses a compact expression format to define schedules for recurring tasks. A standard cron expression consists of five fields separated by spaces, each representing a unit of time. From left to right, the fields are: minute (0-59), hour (0-23), day of the month (1-31), month (1-12), and day of the week (0-6, with 0 representing Sunday). Together, these five fields can describe virtually any recurring schedule, from "every minute" to "at 3:15 PM on the first Monday of March."
The power of cron expressions comes from the special characters that modify each field. The asterisk (*) means "every value" in the given range. A comma (,) creates a list of specific values, such as 1,3,5 for Monday, Wednesday, and Friday. A hyphen (-) defines a range, like 9-17 for hours 9 through 17. A forward slash (/) sets a step interval, where */10 in the minute field means "every 10 minutes." These operators can be combined: 1-5/2 means every 2nd value from 1 to 5, producing 1, 3, and 5.
Common Cron Patterns
Several cron expressions appear so frequently that they are worth memorizing. * * * * * runs every minute and is commonly used during development and testing. 0 * * * * runs at the top of every hour, useful for hourly reports or cache refreshes. 0 0 * * * runs at midnight every day, a typical schedule for daily database backups and log rotation. 0 9 * * 1 runs at 9:00 AM every Monday, perfect for weekly team reports. 0 0 1 * * runs at midnight on the first day of every month, suited for monthly billing or reporting tasks.
Real-World Use Cases
Cron schedules drive essential infrastructure tasks across every type of software system. Database backups typically run during off-peak hours using a schedule like 30 2 * * * (2:30 AM daily). Log rotation and cleanup scripts prevent disk space exhaustion by running daily or weekly. Email digest systems send summary emails on defined schedules, often weekly on Monday mornings. CI/CD pipelines use cron triggers to run nightly builds and integration test suites. Monitoring systems schedule health checks at regular intervals, such as every 5 minutes, to detect outages quickly.
Differences Between Cron Implementations
While the standard 5-field cron expression is universal across Unix systems, several variations exist. The Quartz scheduler, widely used in Java applications, adds a seconds field at the beginning (making 6 or 7 fields total) and supports additional features like the L modifier (last day of month) and the W modifier (nearest weekday). AWS EventBridge and CloudWatch use a similar but distinct syntax with a year field and support for the ? character. Kubernetes CronJobs use the standard 5-field format. When writing cron expressions, always verify which format your specific platform expects.
Tips for Testing Cron Expressions
Always verify cron expressions before deploying them to production. Use a parser like this tool to see the human-readable description and next run times. Pay special attention to the interaction between day-of-month and day-of-week fields: in standard cron, if both are restricted (not *), the job runs when either condition is met, not when both are met. This is a common source of confusion that causes jobs to run more often than expected. Start with a high-frequency schedule during testing (like every minute), verify the job works correctly, then switch to the production schedule. Keep a comment next to every cron entry that describes the schedule in plain English.
Frequently Asked Questions
What is a cron expression?
A cron expression is a five-field string (minute, hour, day-of-month, month, day-of-week) that defines a recurring schedule. Each field uses values, ranges, steps, or wildcards to specify when a task should run.
What does the asterisk (*) mean in cron?
The asterisk means "every possible value" for that field. For example, * in the hour field means every hour, and * * * * * means every minute of every day.
How do step values work (*/5)?
The */N syntax means "every Nth value starting from the minimum." In the minute field, */5 produces 0, 5, 10, 15, ..., 55. You can also use ranges with steps: 1-30/5 means every 5th minute from 1 to 30.
What is the difference between standard and extended cron?
Standard cron uses 5 fields. Extended formats (like Quartz scheduler) add seconds and year fields, plus special characters like L for "last day." This tool supports the standard 5-field Unix cron format.
How do I schedule a job for the last day of every month?
Standard cron has no "last day" syntax. The workaround is to run on days 28-31 with a script that checks if tomorrow is the 1st. Some extended implementations support the L modifier for this purpose.
Save your results & get weekly tips
Get calculator tips, formula guides, and financial insights delivered weekly. Join 10,000+ readers.
No spam. Unsubscribe anytime.