Complete Guide to Cron Expressions & Scheduling
Cron is the time-based job scheduler that powers automation across Unix, Linux, macOS, cloud platforms, and CI/CD pipelines. If you have ever needed to run a script at midnight, send a report every Monday, or clean up logs every hour, you have used cron.
- Master cron expressions with this complete guide.
- Covers 1. anatomy of a cron expression.
- Covers 2. special characters.
- Covers 3. common cron schedules.
- Covers 4. common gotchas.
1. Anatomy of a Cron Expression
A standard cron expression has five fields separated by spaces: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-6, where 0 is Sunday). Each field can be a specific value, a range, a list, a step, or a wildcard.
The five fields answer the question: at what minute, of what hour, on what day of the month, in what month, on what day of the week should this job run?
2. Special Characters
- * (asterisk): Matches every possible value. * in the hour field means every hour.
- / (slash): Step values. */5 in minutes means every 5 minutes. */2 in hours means every 2 hours.
- - (dash): Ranges. 9-17 in hours means 9 AM through 5 PM. 1-5 in day-of-week means Monday through Friday.
- , (comma): Lists. 1,15 in day-of-month means the 1st and 15th. 0,30 in minutes means on the hour and half hour.
3. Common Cron Schedules
- Every minute: * * * * *
- Every 5 minutes: */5 * * * *
- Every hour on the hour: 0 * * * *
- Daily at midnight: 0 0 * * *
- Weekdays at 9 AM: 0 9 * * 1-5
- First of every month: 0 0 1 * *
- Every Sunday at 2 AM: 0 2 * * 0
4. Common Gotchas
Day-of-month AND day-of-week
When both day-of-month and day-of-week are set to non-* values, most cron implementations use OR logic — the job runs when either condition is met. This catches many people off guard. 0 0 15 * 5 means midnight on the 15th OR every Friday, not the 15th only if it is Friday.
-webkit-backdrop-filter alongside backdrop-filter for Safari support. Without the prefix, the effect is invisible to roughly 25% of mobile users.Environment variables
Cron jobs run with a minimal environment — your PATH, HOME, and other variables may not be set. Always use absolute paths in cron scripts and source your profile if needed.
5. Cron Best Practices
- Log everything: Redirect stdout and stderr to a log file. Silent failures are the worst kind.
- Use lock files: Prevent overlapping runs with flock or a PID file, especially for long-running jobs.
- Test at a safe frequency first: Start with */5 * * * * (every 5 minutes) to verify the job works before setting the final schedule.
- Document your crontab: Add comments above each entry explaining what the job does and who owns it.