Cron jobs are the backbone of Linux automation. Whether you need to backup databases, clean temporary files, or run scheduled scripts, cron is there. In this comprehensive tutorial, you'll master cron jobs from basics to advanced techniques.
What is a Cron Job?
A cron job is a scheduled task that runs automatically at specified times on Unix/Linux systems. The name comes from "chron" (Greek for time). Cron is the daemon that runs in the background, executing jobs according to your schedule.
Common Use Cases
- Database backups (daily at 2 AM)
- Log rotation (weekly)
- Email newsletters (monthly)
- System maintenance (every hour)
- API data sync (every 15 minutes)
Understanding Cron Syntax
Cron expressions use 5 fields to define when a job runs:
┌───────────── minute (0-59) │ ┌───────────── hour (0-23) │ │ ┌───────────── day of month (1-31) │ │ │ ┌───────────── month (1-12) │ │ │ │ ┌───────────── day of week (0-6) │ │ │ │ │ * * * * * command to execute
| Field | Allowed Values | Special Characters |
|---|---|---|
| Minute | 0-59 | * , - / |
| Hour | 0-23 | * , - / |
| Day of Month | 1-31 | * , - / |
| Month | 1-12 | * , - / |
| Day of Week | 0-6 (0=Sunday) | * , - / |
Special Characters Explained
* (Asterisk) - Every
Matches all values. * in the hour field means "every hour".
0 * * * * # Runs every hour at minute 0
, (Comma) - List
Specifies multiple values. 1,3,5 in day of week means Monday, Wednesday, Friday.
0 9 * * 1,3,5 # 9 AM on Mon, Wed, Fri
- (Hyphen) - Range
Specifies a range. 9-17 in hour field means 9 AM to 5 PM.
0 9-17 * * * # Every hour from 9 AM to 5 PM
/ (Slash) - Step
Specifies increments. */15 in minute field means "every 15 minutes".
*/15 * * * * # Every 15 minutes
Common Cron Schedules
| Schedule | Cron Expression | Description |
|---|---|---|
| Every minute | * * * * * |
Runs every minute |
| Every 5 minutes | */5 * * * * |
Runs every 5 minutes |
| Every hour | 0 * * * * |
Runs at minute 0 of every hour |
| Daily at midnight | 0 0 * * * |
Runs every day at 12:00 AM |
| Daily at 2:30 AM | 30 2 * * * |
Runs every day at 2:30 AM |
| Weekly (Sunday) | 0 0 * * 0 |
Runs every Sunday at midnight |
| Monthly (1st) | 0 0 1 * * |
Runs on the 1st of every month |
| Weekdays 9 AM | 0 9 * * 1-5 |
Runs Mon-Fri at 9 AM |
How to Create Cron Jobs
Step 1: Open Crontab
crontab -e
This opens your user's crontab file in your default editor.
Step 2: Add Your Job
# Backup database daily at 2 AM 0 2 * * * /home/user/scripts/backup.sh
Step 3: Save and Exit
Save the file. Cron automatically loads the new schedule.
Step 4: Verify
crontab -l # List current cron jobs
⚠️ Important: File Permissions
Make sure your script is executable:
chmod +x /home/user/scripts/backup.sh
Real-World Examples
1. Daily Database Backup
# Backup MySQL database every day at 2 AM 0 2 * * * mysqldump -u user -p'password' database > /backups/db-$(date +\%F).sql
2. Weekly Log Cleanup
# Delete logs older than 7 days, every Sunday at 3 AM 0 3 * * 0 find /var/log -name "*.log" -mtime +7 -delete
3. Hourly API Sync
# Sync data from API every hour 0 * * * * /home/user/scripts/sync-api.sh >> /var/log/sync.log 2>&1
4. Monthly Report Generation
# Generate monthly report on 1st at 6 AM 0 6 1 * * /home/user/scripts/monthly-report.sh
Cron Best Practices
1. Use Absolute Paths
# ❌ Bad 0 2 * * * backup.sh # ✅ Good 0 2 * * * /home/user/scripts/backup.sh
2. Redirect Output
# Log output and errors 0 2 * * * /script.sh >> /var/log/script.log 2>&1
3. Set PATH Variable
PATH=/usr/local/bin:/usr/bin:/bin 0 2 * * * /script.sh
4. Test Before Deploying
Run your script manually first to ensure it works:
/home/user/scripts/backup.sh
5. Use Comments
# Daily database backup at 2 AM # Author: John Doe # Last updated: 2026-02-18 0 2 * * * /home/user/scripts/backup.sh
Debugging Cron Jobs
Check Cron Logs
# Ubuntu/Debian grep CRON /var/log/syslog # CentOS/RHEL grep CRON /var/log/cron
Verify Cron is Running
systemctl status cron # Ubuntu/Debian systemctl status crond # CentOS/RHEL
Common Issues
- Job not running: Check cron daemon status
- Command not found: Use absolute paths
- Permission denied: Make script executable
- Wrong time: Check server timezone
Need Help Creating Cron Expressions?
Use our free Cron Job Generator to create cron expressions visually. No memorizing syntax required!
Try Cron Generator →Cron Alternatives
systemd Timers
Modern alternative to cron on systemd-based systems. More powerful but more complex.
Anacron
Runs jobs that were missed (e.g., if computer was off). Good for laptops.
Cloud-Based Schedulers
- AWS EventBridge
- Google Cloud Scheduler
- GitHub Actions (for CI/CD)
Conclusion
Cron jobs are essential for automating repetitive tasks on Linux systems. Once you understand the syntax and best practices, you can automate almost anything.
Key takeaways:
- Cron uses 5 fields: minute, hour, day, month, weekday
- Special characters:
*(every),,(list),-(range),/(step) - Always use absolute paths in cron jobs
- Redirect output to logs for debugging
- Test scripts manually before adding to cron