Automation is key to efficient system administration. Linux provides multiple tools for scheduling and automating tasks.
Cron - Traditional Scheduler
Crontab Format
* * * * * command
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, Sunday = 0 or 7)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)
Examples
# Edit crontab
crontab -e
# Examples
0 2 * * * /path/to/backup.sh # Daily at 2 AM
*/15 * * * * /path/to/check.sh # Every 15 minutes
0 0 1 * * /path/to/monthly.sh # First day of month
Special Strings
@reboot # Run at boot
@daily # Once per day
@weekly # Once per week
@monthly # Once per month
@yearly # Once per year
systemd Timers
Modern alternative to cron with better logging and dependencies:
Timer File
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
OnBootSec=10min
[Install]
WantedBy=timers.target
Service File
[Unit]
Description=Daily Backup Service
[Service]
Type=oneshot
ExecStart=/path/to/backup.sh
Management
systemctl enable --now backup.timer
systemctl list-timers
systemctl status backup.timer
Ansible for Automation
For complex automation across multiple systems:
- name: Update packages
apt:
update_cache: yes
upgrade: dist
become: yes
Best Practices
- Test scripts before scheduling
- Log all automated tasks
- Use systemd timers for new systems
- Document what each automation does
- Set up monitoring for automated tasks
Automation reduces manual work and ensures consistency across systems.