Cron Expressions — Complete Guide to Scheduling Tasks
Cron Expressions — Scheduling Tasks with Precision on Any Server
Cron is the time-based job scheduler present on every Unix and Linux system. It runs tasks (called cron jobs) at specific times, intervals, or schedules defined by cron expressions — a compact syntax that can represent everything from “every minute” to “at 2:30 AM on the first Monday of March and September.” If you manage a server, you will eventually need to schedule something — database backups, log rotation, report generation, cache clearing, or system health checks — and cron is how it gets done.
The Five-Field Cron Syntax
A standard cron expression has five fields, separated by spaces: minute hour day-of-month month day-of-week
- Minute: 0-59
- Hour: 0-23
- Day of month: 1-31
- Month: 1-12
- Day of week: 0-7 (0 and 7 both represent Sunday)
Special characters:
*— any value (every minute, every hour, etc.),— list of values (1,15 means the 1st and 15th)-— range (1-5 means Monday through Friday)/— step values (*/15 means every 15 units)
Common Cron Schedules with Examples
0 * * * * — Every hour, on the hour (minute 0 of every hour)
*/15 * * * * — Every 15 minutes
0 2 * * * — Daily at 2:00 AM (database backups, log rotation)
0 9 * * 1-5 — Weekdays at 9:00 AM (daily business reports)
0 0 1 * * — First day of every month at midnight (monthly reports)
0 0 * * 0 — Every Sunday at midnight (weekly maintenance)
30 6 15 1,4,7,10 * — Quarterly: 6:30 AM on the 15th of Jan, Apr, Jul, Oct
Common Mistakes with Cron
Timezone confusion: Cron runs in the server’s local timezone, which may differ from your timezone or your users’ timezone. A job scheduled for “0 9 * * *” runs at 9 AM server time — if your server is in UTC and your users are in IST (UTC+5:30), that is 2:30 PM IST. Always note the server timezone when setting up cron jobs.
Output handling: By default, cron emails the output of every job to the system user. On a server running dozens of cron jobs, this generates hundreds of emails daily. Redirect output to log files: 0 2 * * * /backup.sh >> /var/log/backup.log 2>&1
Environment differences: Cron jobs run with a minimal environment — your PATH, shell aliases, and environment variables from your interactive shell are not available. Use full paths to commands (/usr/bin/python3, not just python3) and source any required environment files at the start of your script.
Overlapping executions: If a job scheduled every 5 minutes takes 8 minutes to complete, two instances will overlap. Use lock files or tools like flock to prevent concurrent execution: */5 * * * * flock -n /tmp/myjob.lock /path/to/script.sh
Build and validate cron expressions with our Cron Expression Generator — select your schedule visually and get the correct cron syntax with a human-readable explanation.
Understanding Cron Expression Components
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-7, where both 0 and 7 represent Sunday). Each field accepts specific values, ranges (1-5), lists (1,3,5), and intervals (using slash notation like */15 for every 15 minutes). The asterisk wildcard means every possible value for that field.
Some common patterns worth memorizing: 0 0 * * * runs at midnight daily. 0 */2 * * * runs every 2 hours. 30 9 * * 1-5 runs at 9:30 AM on weekdays. 0 0 1 * * runs at midnight on the first of each month. 0 0 * * 0 runs at midnight every Sunday. Our Cron Expression Generator at toolshake.com lets you build expressions visually and shows you the next 10 execution times so you can verify the schedule is correct before deploying.