Cron Job Guide: Understanding Cron Expressions with Examples
Cron is a time-based job scheduler in Unix-like operating systems. Whether you're automating backups, sending reports, or running maintenance scripts, understanding cron expressions is essential for any developer or sysadmin.
The Five-Field Cron Syntax
A standard cron expression has five fields, separated by spaces:
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6, Sunday = 0)
│ │ │ │ │
* * * * *
Special Characters
| Character | Meaning | Example |
|---|---|---|
* | Every value | * * * * * = every minute |
, | List of values | 1,15 * * * * = minute 1 and 15 |
- | Range | * 9-17 * * * = hours 9 to 17 |
/ | Step | */5 * * * * = every 5 minutes |
Common Cron Schedule Examples
Frequent Intervals
* * * * * # Every minute
*/5 * * * * # Every 5 minutes
*/15 * * * * # Every 15 minutes
*/30 * * * * # Every 30 minutes
0 * * * * # Every hour (at minute 0)
0 */2 * * * # Every 2 hours
0 */6 * * * # Every 6 hours
Daily Schedules
0 0 * * * # Daily at midnight
0 6 * * * # Daily at 6:00 AM
30 8 * * * # Daily at 8:30 AM
0 12 * * * # Daily at noon
0 23 * * * # Daily at 11:00 PM
Weekly Schedules
0 0 * * 0 # Every Sunday at midnight
0 9 * * 1 # Every Monday at 9:00 AM
0 9 * * 1-5 # Weekdays at 9:00 AM
0 18 * * 5 # Every Friday at 6:00 PM
0 10 * * 6,0 # Weekends at 10:00 AM
Monthly and Yearly
0 0 1 * * # First day of every month at midnight
0 9 15 * * # 15th of every month at 9:00 AM
0 0 1 1 * # January 1st at midnight (yearly)
0 0 1 */3 * # First day of every quarter
Setting Up Cron Jobs
Edit Your Crontab
# Open the crontab editor
crontab -e
# List current cron jobs
crontab -l
# Remove all cron jobs (careful!)
crontab -r
Example: Backup Script
# Run backup every day at 2:00 AM
0 2 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1
# Run database backup every 6 hours
0 */6 * * * /usr/local/bin/db-backup.sh
# Clean temp files every Sunday at 3:00 AM
0 3 * * 0 find /tmp -type f -mtime +7 -delete
Environment Variables in Cron
Cron runs with a minimal environment. Common issues arise because PATH is limited. Always use full paths:
# Bad (might not find the command)
0 * * * * python script.py
# Good (full paths)
0 * * * * /usr/bin/python3 /home/user/script.py
# Or set PATH at the top of crontab
PATH=/usr/local/bin:/usr/bin:/bin
0 * * * * python3 /home/user/script.py
Logging and Debugging
# Redirect output to a log file
0 * * * * /home/user/script.sh >> /var/log/myscript.log 2>&1
# Send output to /dev/null (suppress)
0 * * * * /home/user/script.sh > /dev/null 2>&1
# Check cron logs
grep CRON /var/log/syslog # Debian/Ubuntu
grep cron /var/log/cron # CentOS/RHEL
Common Mistakes
- Forgetting output redirection — Cron sends stdout/stderr as email by default
- Relative paths — Always use absolute paths for scripts and commands
- Missing execute permissions — Run
chmod +x script.sh - Wrong timezone — Cron uses system timezone by default
- Percentage signs —
%is special in crontab; escape with\%
Alternatives to Cron
- systemd timers — Modern alternative on systemd-based Linux
- at — One-time scheduled jobs
- GitHub Actions schedules — CI/CD cron schedules
- Cloud schedulers — AWS EventBridge, GCP Cloud Scheduler
🛠 Try it yourself: Use our Cron Expression Builder to create and validate cron expressions visually, with next-run previews.