Introduction to Cron
Cron is Linux’s built-in job scheduler that automates repetitive tasks like backups, updates, or custom scripts. The crontab
(cron table) file defines when and how these tasks run. Whether you’re a developer or sysadmin, mastering crontab unlocks powerful automation!
Accessing Your Crontab
-
Open crontab:
crontab -e
- First run? Choose a text editor (nano recommended for beginners).
-
View active jobs:
crontab -l
-
User-specific: Jobs run under your account. Need system-wide tasks? Use
/etc/crontab
(requiressudo
).
Crontab Syntax Explained
Each line follows this structure:
* * * * * /path/to/command arg1 arg2
Field | Allowed Values | Description |
---|---|---|
1 | 0-59 or * | Minute (0-59) |
2 | 0-23 or * | Hour (0-23) |
3 | 1-31 or * | Day of month (1-31) |
4 | 1-12 or * | Month (1-12) |
5 | 0-7 (0=Sun,7=Sun) or * | Day of week (0-7) |
6+ | User-defined | Command to execute |
Practical Examples
-
Daily backup at 3:30 AM:
30 3 * * * /home/user/backup.sh
-
Every Monday at noon:
0 12 * * 1 /usr/bin/update-script
-
Every 10 minutes:
*/10 * * * * /path/to/monitor-service
-
On reboot:
@reboot /scripts/start-server.sh
Special Shortcuts
Use these instead of complex schedules:
@yearly
: Runs once a year (0 0 1 1 *)@monthly
: First day of month at midnight (0 0 1 )@weekly
: Sunday at midnight (0 0 0)@daily
/@midnight
: 00:00 daily@hourly
: Minute 0 every hour@reboot
: On system startup
Pro Tips & Troubleshooting
✅ Path issues:
Always use full paths in commands (e.g., /usr/bin/python3
instead of python3
).
✅ Capture outputs:
Redirect logs to debug:
* * * * * /job.sh >> /logs/job.log 2>&1
✅ Permissions:
Ensure scripts are executable:
chmod +x /path/to/your-script.sh
✅ Reload changes:
Cron automatically reloads after saving (crontab -e
).
🚨 Test first:
Run commands manually before automating!
Managing Crontabs
- Delete all jobs:
crontab -r
- Edit another user’s crontab (admin only):
sudo crontab -u username -e
- Backup your crontab:
crontab -l > cron_backup.txt
Conclusion
Crontab is your gateway to effortless Linux automation. Start with simple schedules, leverage shortcuts, and remember:
> “Automation doesn’t replace humans; it liberates them from repetition.”
Experiment safely, check logs (/var/log/syslog
for cron activity), and enjoy your newfound efficiency! 🚀
Next step: Try automating your first task right now!