Cron is the built-in Linux task scheduler that runs commands at specified intervals. Whether you need to automate backups, rotate logs, send reports, or run maintenance tasks, crontab is the tool for the job. This guide covers the cron syntax in depth with 30+ real-world scheduling examples you can copy and adapt for your own servers.
Crontab Syntax Explained
# Cron format:
# +---------------- minute (0-59)
# | +------------- hour (0-23)
# | | +---------- day of month (1-31)
# | | | +------- month (1-12)
# | | | | +---- day of week (0-7, 0 and 7 = Sunday)
# | | | | |
# * * * * * command_to_execute
# Special characters:
# * Any value
# , Value list (1,3,5)
# - Range (1-5)
# / Step values (*/5 = every 5)
Managing Your Crontab
# Edit your crontab
crontab -e
# List your cron jobs
crontab -l
# Edit another user's crontab (requires root)
sudo crontab -u www-data -e
# Remove all cron jobs (be careful!)
crontab -r
# System-wide crontab
sudo nano /etc/crontab
# Cron directories (drop scripts here, no crontab syntax needed)
ls /etc/cron.daily/
ls /etc/cron.hourly/
ls /etc/cron.weekly/
ls /etc/cron.monthly/
1-10: Basic Time Schedules
# 1. Every minute
* * * * * /opt/scripts/health-check.sh
# 2. Every 5 minutes
*/5 * * * * /opt/scripts/monitor.sh
# 3. Every 15 minutes
*/15 * * * * /opt/scripts/sync-data.sh
# 4. Every hour (at minute 0)
0 * * * * /opt/scripts/hourly-report.sh
# 5. Every 2 hours
0 */2 * * * /opt/scripts/cleanup.sh
# 6. Daily at midnight
0 0 * * * /opt/scripts/daily-backup.sh
# 7. Daily at 2:30 AM (best for maintenance)
30 2 * * * /opt/scripts/maintenance.sh
# 8. Daily at 6 AM and 6 PM
0 6,18 * * * /opt/scripts/sync.sh
# 9. Every weekday (Mon-Fri) at 9 AM
0 9 * * 1-5 /opt/scripts/business-report.sh
# 10. Every Sunday at midnight
0 0 * * 0 /opt/scripts/weekly-backup.sh
11-20: Advanced Scheduling Patterns
# 11. First day of every month at 3 AM
0 3 1 * * /opt/scripts/monthly-report.sh
# 12. Every quarter (Jan, Apr, Jul, Oct) on the 1st
0 0 1 1,4,7,10 * /opt/scripts/quarterly-audit.sh
# 13. Last day of the month (clever trick)
0 0 28-31 * * [ "$(date -d tomorrow +\%d)" = "01" ] && /opt/scripts/month-end.sh
# 14. Every 30 minutes during business hours only
*/30 9-17 * * 1-5 /opt/scripts/business-monitor.sh
# 15. Twice daily at 8 AM and 8 PM
0 8,20 * * * /opt/scripts/check-updates.sh
# 16. Every Saturday and Sunday at noon
0 12 * * 6,0 /opt/scripts/weekend-maintenance.sh
# 17. Every 10 minutes during the first hour of each day
*/10 0 * * * /opt/scripts/overnight-batch.sh
# 18. At system reboot (special string)
@reboot /opt/scripts/startup.sh
# 19. Once a year (midnight, January 1st)
@yearly /opt/scripts/annual-cleanup.sh
# 20. Once a week (midnight, Sunday)
@weekly /opt/scripts/weekly-digest.sh
21-30: Production Use Cases
# 21. Automated database backup (daily 2 AM, compressed)
0 2 * * * pg_dump -U postgres mydb | gzip > /backup/db-$(date +\%Y\%m\%d).sql.gz 2>> /var/log/backup.log
# 22. Delete old log files (older than 30 days)
0 0 * * * find /var/log/app/ -name "*.log" -mtime +30 -delete
# 23. SSL certificate renewal check (weekly Monday 3 AM)
0 3 * * 1 certbot renew --quiet --post-hook "systemctl reload nginx"
# 24. Disk usage alert email
*/30 * * * * [ $(df / --output=pcent | tail -1 | tr -d ' %%') -gt 80 ] && echo "Disk alert on $(hostname)" | mail -s "Disk Alert" admin@example.com
# 25. Clear tmp files older than 7 days
0 4 * * * find /tmp -type f -atime +7 -delete 2>/dev/null
# 26. Automatic system updates (Sunday 3 AM)
0 3 * * 0 apt update && apt upgrade -y >> /var/log/auto-update.log 2>&1
# 27. Website uptime check (every 2 minutes)
*/2 * * * * curl -sf https://mysite.com > /dev/null || echo "Site DOWN at $(date)" >> /var/log/uptime.log
# 28. Sync files to backup server (every 6 hours)
0 */6 * * * rsync -az --delete /var/www/ backup@remote:/backup/www/
# 29. Generate and email daily report (Mon-Fri 7 AM)
0 7 * * 1-5 php /var/www/app/scripts/daily-report.php 2>&1 | mail -s "Daily Report" team@example.com
# 30. Restart a memory-leaking application (daily 4 AM)
0 4 * * * systemctl restart myapp && echo "$(date): Restarted myapp" >> /var/log/myapp-restart.log
Cron Output, Logging, and Email
# Redirect all output to a log file
*/5 * * * * /opt/scripts/job.sh >> /var/log/cronjob.log 2>&1
# Suppress ALL output (silent mode)
*/5 * * * * /opt/scripts/job.sh > /dev/null 2>&1
# Send output via email (set MAILTO at top of crontab)
MAILTO=admin@example.com
0 8 * * * /opt/scripts/report.sh
# Disable email for one specific job
0 * * * * /opt/scripts/quiet-job.sh > /dev/null 2>&1
# Set environment variables in crontab
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/root
MAILTO=admin@example.com
Troubleshooting Cron Jobs
# Check if cron service is running
systemctl status cron
# View recent cron execution logs
grep CRON /var/log/syslog | tail -20
journalctl -u cron --since "1 hour ago"
# Common cron problems and solutions:
# 1. Missing PATH - always use full paths (/usr/bin/php not just php)
# 2. Missing environment - cron runs with minimal env, source .bashrc if needed
# 3. Wrong permissions - scripts must be executable (chmod +x)
# 4. Relative paths - always use absolute paths in cron
# 5. Percent signs - escape %% as \%% in crontab lines
# 6. No output - redirect stderr too: 2>&1
Cron vs. Systemd Timers
| Feature | Cron | Systemd Timer |
|---|---|---|
| Setup | Simple one-liner | Two unit files |
| Logging | Manual setup | Built-in journalctl |
| Dependencies | None | Full dependency management |
| Catch-up | Missed jobs are skipped | Persistent=true catches up |
| Best for | Quick scripts, simple tasks | Complex services, deps |
Recommended Reading
Master Linux automation and system administration:
Download our Crontab Examples Cheat Sheet for a printable wall reference with all 30+ schedules and the cron syntax guide.