Scheduling Tasks with Cron
Cron allows you to schedule commands to run automatically at set intervals.
Edit the Crontab
crontab -e
Crontab Syntax
* * * * * command
| | | | +-- Day of week (0=Sunday, 7=Sunday)
| | | +---- Month (1-12)
| | +------ Day of month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)
Examples
# Run a backup script every day at 2 AM
0 2 * * * /root/backup.sh
# Restart nginx every Sunday at midnight
0 0 * * 0 systemctl restart nginx
# Run a script every 5 minutes
*/5 * * * * /home/user/check.sh
View Current Crontab
crontab -l
System-Wide Cron Jobs
Place scripts in /etc/cron.daily/, /etc/cron.weekly/, or /etc/cron.monthly/ for automatic execution.