What is Cron?
Cron is the standard Linux task scheduler. It runs commands at specified times and intervals, making it essential for backups, log rotation, maintenance scripts, and more.
Editing Crontab
# Edit current user crontab
crontab -e
# Edit root crontab
sudo crontab -e
# List current crontab
crontab -lCrontab Syntax
# ┌───── minute (0-59)
# │ ┌───── hour (0-23)
# │ │ ┌───── day of month (1-31)
# │ │ │ ┌───── month (1-12)
# │ │ │ │ ┌───── day of week (0-7, 0 and 7 = Sunday)
# │ │ │ │ │
# * * * * * commandCommon Examples
# Every 5 minutes
*/5 * * * * /root/scripts/check.sh
# Daily at 3:30 AM
30 3 * * * /root/scripts/backup.sh
# Every Monday at 9 AM
0 9 * * 1 /root/scripts/weekly-report.sh
# First day of each month at midnight
0 0 1 * * /root/scripts/monthly-cleanup.sh
# Every hour during business hours (9-17), weekdays only
0 9-17 * * 1-5 /root/scripts/sync.shSpecial Strings
@reboot /root/scripts/startup.sh # Run once at boot
@hourly /root/scripts/hourly.sh # 0 * * * *
@daily /root/scripts/daily.sh # 0 0 * * *
@weekly /root/scripts/weekly.sh # 0 0 * * 0
@monthly /root/scripts/monthly.sh # 0 0 1 * *Best Practices
- Always redirect output:
command >> /var/log/cron.log 2>&1 - Use absolute paths for commands and scripts
- Test scripts manually before adding to cron
- Use
MAILTO=admin@example.comat the top to receive error notifications