Automation is key to efficient Linux system administration. Job scheduling allows tasks like backups, maintenance, and monitoring to run automatically without manual intervention. This guide covers all major Linux scheduling tools—cron, at, batch, and systemd timers—with examples, best practices, and troubleshooting tips.
Cron Jobs
Cron uses the crond daemon to run recurring tasks defined in crontab files.
- User crontabs: Each user has a personal crontab.
- System-wide crontabs: Found in /etc/crontab or /etc/cron.d/.
Crontab Format
* * * * * command
Fields:
- Minute (0-59)
- Hour (0-23)
- Day of Month (1-31)
- Month (1-12)
- Day of Week (0-7, Sunday=0 or 7)
Special characters:
- * → every value
- , → list of values (e.g., 1,15)
- - → range (e.g., 1-5 weekdays)
- / → step values (e.g., */10 every 10 minutes)
Predefined aliases:
- @reboot – runs at system boot
- @daily – runs daily at midnight
- @hourly – runs every hour
- @weekly – runs weekly
- @monthly – runs monthly
- @yearly – runs yearly
Common Commands
crontab -e # Edit current user's crontab
crontab -l # List scheduled jobs
crontab -r # Remove all jobs
Examples
| Expression | Meaning |
|---|---|
0 5 * * * | 5:00 AM daily |
*/15 * * * * | Every 15 minutes |
0 0 1 * * | Midnight on 1st of each month |
0 22 * * 1-5 | 10 PM weekdays (Mon-Fri) |
@reboot /script.sh | On system boot |
Environment variables like SHELL, PATH, and MAILTO in /etc/crontab control execution context. Logs appear in /var/log/cron.
At and Batch Jobs
at schedules one-time tasks for a specific future time:
# echo "/backup.sh" | at 18:30 tomorrow
Time formats: 18:30 tomorrow, noon + 2 hours, teatime.
Batch runs jobs when system load is low (default load average <1.5), ideal for heavy tasks.
Useful Commands
# atq # View pending jobs
# atrm job_id # Remove job
Systemd Timers
Modern Linux systems (RHEL 7+, Ubuntu 16.04+) prefer systemd timers for better integration, logging, and dependency management.
Steps to Create a Timer
Create a service file (myjob.service) with the command to run.
Create a timer file (myjob.timer) with scheduling:
[Timer]
OnCalendar=*-*-* 03:30:00 # Daily at 3:30 AM
OnBootSec=5min # 5 minutes after boot
OnUnitActiveSec=1d # Every 24 hours
Persistent=true # Run missed jobs after reboot
AccuracySec=1m # Tolerance of 1 minute
Enable and start:
# systemctl enable myjob.timer
# systemctl start myjob.timer
# systemctl list-timers
Logs are available via journalctl for precise debugging.
Best Practices
- Use full paths for scripts: /usr/bin/script.sh.
- Redirect output to log files: command > /logfile 2>&1.
- Test scripts manually before scheduling.
- Avoid overloading the system with frequent jobs; monitor using top or htop.
- Secure crontabs: chmod 600 /path/to/crontab_file.
- Consider tools like Cronitor for production monitoring of job failures.
Troubleshooting Common Issues
- Job not running: Check crond status (systemctl status crond), permissions, and syntax (crontab -l).
- No output: Set MAILTO=user@domain or log explicitly.
- Time zones: Cron uses system time; verify with timedatectl.
- Permissions: Scripts need execute bit (chmod +x); root jobs in /etc/cron.d/ require user field.
This setup ensures reliable automation across Linux distributions.
Conclusion
Linux job scheduling is essential for reliable automation. Whether using cron for recurring tasks, at/batch for one-time or low-load jobs, or systemd timers for modern, integrated automation, proper scheduling reduces manual effort, ensures consistency, and keeps systems running smoothly.
No comments:
Post a Comment