Cron Expression Cheat Sheet
Cron expressions are a powerful way to schedule background tasks, scripts, and jobs in Unix-like operating systems and modern cloud environments. This guide will walk you through the 5-field cron syntax, special characters, and provide practical examples.
The 5-Field Cron Syntax
A standard cron expression consists of five fields separated by spaces:
* * * * *
| | | | |
| | | | +-- Day of Week (0 - 7) (Sunday=0 or 7)
| | | +---- Month (1 - 12)
| | +------ Day of Month (1 - 31)
| +-------- Hour (0 - 23)
+---------- Minute (0 - 59)
Special Characters
*(Asterisk): Matches all values for a field. For example,*in the minute field means "every minute".,(Comma): Used to separate items in a list. For example,1,15,30in the minute field means the 1st, 15th, and 30th minutes.-(Hyphen): Defines a range. For example,9-17in the hour field means from 9 AM to 5 PM./(Slash): Specifies step values. For example,*/5in the minute field means every 5 minutes.
Common Cron Patterns
| Description | Expression |
|---|---|
| Every minute | * * * * * |
| Every 5 minutes | */5 * * * * |
| Every hour, on the hour | 0 * * * * |
| Every day at midnight | 0 0 * * * |
| Every Monday at 9:00 AM | 0 9 * * 1 |
| The 1st day of every month at midnight | 0 0 1 * * |
Real-World Examples
Database Backups
Run a database backup every day at 2:30 AM:
30 2 * * * /usr/local/bin/backup.sh
Log Rotation
Rotate server logs every Sunday at midnight:
0 0 * * 0 /usr/sbin/logrotate /etc/logrotate.conf
Email Reports
Send a weekly summary report on Friday at 5:00 PM:
0 17 * * 5 /opt/scripts/send_report.py
Troubleshooting Common Mistakes
- Timezones: Remember that cron runs in the system's local timezone unless specified otherwise. In cloud environments (like AWS or Kubernetes), this is usually UTC.
- Missing Paths: Always use absolute paths in your cron commands (e.g.,
/usr/bin/python3instead ofpython3) because cron has a very minimal$PATHenvironment. - Syntax Errors: Ensure you have exactly 5 (or 6 for some extended parsers) fields. Using a cron visualizer can help you catch off-by-one errors.
Need help building or testing your cron expressions? Try our Cron Visualizer tool.