๐ง Linux Crontab & System Scheduling Complete Guide: Cron & systemd-timer Practical Tutorial
Task scheduling is a fundamental component of Linux system administration. This guide provides a complete and practical explanation of crontab, cron daemon, and systemd timers. With real-world examples, you will learn how to automate backups, health checks, log cleanup, and more.
๐ 1. Overview of Cron and Crontab
Cron is the system scheduler that runs tasks at specified times. Crontab is the configuration file where users define their scheduled jobs.
cron: the background service that triggers tasks.crontab: per-user or system-wide schedule definitions.- Locations:
/etc/crontab/etc/cron.d/,/etc/cron.hourly/, etc.crontab -efor user-specific schedules
๐ 2. Crontab Format (5 Fields)
The standard crontab syntax includes 5 fields:
# Min Hour Day Month Weekday
* * * * * command
| Field | Values | Description |
|---|---|---|
| Minute | 0–59 | Minute of the hour |
| Hour | 0–23 | Hour of the day |
| Day | 1–31 | Day of the month |
| Month | 1–12 | Month |
| Weekday | 0–7 | 0/7 = Sunday |
✔ Common symbols
* all values
, list of values (e.g., 1,3,5)
- range (e.g., 1-5)
/ step value (e.g., */10 = every 10 minutes)
๐ 3. Practical Crontab Examples
1️⃣ Daily backup at 02:00
0 2 * * * /usr/local/bin/backup.sh
2️⃣ Run health check every 10 minutes
*/10 * * * * systemctl is-active nginx >> /var/log/nginx_health.log
3️⃣ Weekly cleanup of logs older than 7 days
0 3 * * 1 find /var/log -type f -mtime +7 -delete
๐ Check cron execution logs
journalctl -u cron
# or
grep CRON /var/log/syslog
๐งฉ 4. Systemd Timers: Modern Replacement for Cron
For modern Linux systems (CentOS 7+, RHEL, Ubuntu 18+), systemd timers are recommended. They provide better logging, dependency control, stronger reliability, and more flexible scheduling.
๐ฅ 5. Creating a Systemd Service + Timer
1️⃣ Create the Service
# /etc/systemd/system/cleanup.service
[Unit]
Description=Cleanup old logs
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
2️⃣ Create the Timer
# /etc/systemd/system/cleanup.timer
[Unit]
Description=Run cleanup job daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
3️⃣ Enable the timer
systemctl daemon-reload
systemctl enable --now cleanup.timer
systemctl status cleanup.timer
๐ Check execution logs
journalctl -u cleanup.service
systemctl list-timers
⚖️ 6. Cron vs Systemd Timer Comparison
| Feature | Cron | Systemd Timer |
|---|---|---|
| Logging | syslog only | journalctl full logs |
| Stability | Basic | High; supports dependencies |
| Complex tasks | Harder | Easier and scalable |
| System integration | Low | Full systemd integration |
Recommendation: Use systemd timers for modern systems.
๐ง 7. Troubleshooting
1️⃣ Cron not running?
systemctl status cron
crontab -l
grep CRON /var/log/syslog
2️⃣ Commands not executing (PATH problems)
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
# Add this at the top of your crontab
3️⃣ Timer not triggering?
systemctl status yourjob.timer
systemctl list-timers
journalctl -u yourjob.service
๐ Conclusion
Cron and systemd timers are essential components of Linux automation. Cron is simple and effective for lightweight tasks, while systemd timers offer stability, dependency control, and better observability for modern deployments. For new environments, systemd timers are highly recommended for long-term maintainability.
๐ Related Reading
- Linux systemd Deep Dive
- Linux Logrotate Advanced Guide
- Linux User & Permission ACL Guide
- Linux Server Hardening Complete Guide
— WWFandy・Linux Automation Notes
ๆฒๆ็่จ:
ๅผต่ฒผ็่จ