Chapter 8.3 - Advanced Cron and systemd Timers
Learning Objectives
By the end of this chapter, you will be able to:
- Use system cron (/etc/crontab, /etc/cron.d)
- Understand anacron
- Create systemd timers
- Compare cron vs systemd timers
- Manage cron logs
- Implement advanced scheduled tasks
System Cron
/etc/crontab
Format with user:
# /etc/crontab
# Format: MIN HOUR DAY MONTH DAY_OF_WEEK USER COMMAND
# Example
0 2 * * * root /usr/local/bin/backup.sh
*/15 * * * * www-data /usr/local/bin/check.sh
Edit:
sudo nano /etc/crontab
/etc/cron.d/
Directory for system cron files:
# Create file
sudo nano /etc/cron.d/my-task
# Format (with user)
0 2 * * * user1 /usr/local/bin/script.sh
# Important permissions
sudo chmod 644 /etc/cron.d/my-task
Advantages:
- Organization by file
- Easy to manage
- No need for crontab -e
Special Directories
/etc/cron.hourly/ # Executed every hour (00:00)
/etc/cron.daily/ # Executed every day (06:25)
/etc/cron.weekly/ # Executed every week (Sunday 06:47)
/etc/cron.monthly/ # Executed every month (1st at 06:52)
Place executable scripts (without .sh extension)
Anacron
For Non 24/7 Systems
Anacron executes tasks even if the system was off.
# Configuration
/etc/anacrontab
# Format
# period delay identifier command
1 5 backup /usr/local/bin/backup.sh
7 10 weekly-clean /usr/local/bin/cleanup.sh
Functioning:
- Checks if task executed within period
- If not, executes after delay
- Ideal for laptops, intermittent systems
systemd Timers
Modern Alternative to Cron
Advantages:
- systemd integration
- Logs with journalctl
- Dependencies between units
- More flexible
Create a Timer
Service:
# /etc/systemd/system/backup.service
[Unit]
Description=Daily Backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
User=backup
StandardOutput=journal
StandardError=journal
Timer:
# /etc/systemd/system/backup.timer
[Unit]
Description=Timer for Daily Backup
Requires=backup.service
[Timer]
OnCalendar=daily
OnCalendar=02:00
Persistent=true
[Install]
WantedBy=timers.target
Activate:
sudo systemctl daemon-reload
sudo systemctl enable backup.timer
sudo systemctl start backup.timer
OnCalendar Syntax
# Format: DayOfWeek Year-Month-Day Hour:Minute:Second
# Every day at 2 AM
OnCalendar=daily
OnCalendar=02:00
# Every Monday at 9 AM
OnCalendar=Mon *-*-* 09:00:00
# The 1st of every month
OnCalendar=monthly
OnCalendar=*-*-01 00:00:00
# Every hour
OnCalendar=hourly
# Every 15 minutes
OnCalendar=*:0/15
Timer Options
[Timer]
# Scheduling
OnCalendar=daily
OnBootSec=10min
OnUnitActiveSec=1h
# Persistence (execute if missed)
Persistent=true
# Accuracy (approximate execution)
AccuracySec=1min
# Randomization
RandomizedDelaySec=5min
Cron vs Timers Comparison
| Feature | Cron | systemd Timers |
|---|---|---|
| Logs | Separate files | journalctl |
| Dependencies | No | Yes |
| Persistence | No | Yes (option) |
| Flexibility | Limited | High |
| Simplicity | Simple | More complex |
| Availability | Everywhere | systemd required |
Log Management
Cron Logs
# System logs
/var/log/syslog # Ubuntu/Debian
/var/log/cron # Red Hat/CentOS
# View cron logs
sudo grep CRON /var/log/syslog
sudo tail -f /var/log/syslog | grep CRON
systemd Timers Logs
# Timer logs
sudo journalctl -u backup.timer
# Service logs
sudo journalctl -u backup.service
# Follow
sudo journalctl -u backup.timer -f
# Since date
sudo journalctl -u backup.timer --since "2024-01-01"
Advanced Examples
Backup with Timer
Service:
[Unit]
Description=Automatic Backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
User=backup
StandardOutput=journal
StandardError=journal
Timer:
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
OnCalendar=02:00
Persistent=true
RandomizedDelaySec=30min
[Install]
WantedBy=timers.target
Cleanup with Cron
# /etc/cron.daily/cleanup
#!/bin/bash
# Daily cleanup
find /var/log -name "*.log" -mtime +30 -delete
find /tmp -type f -mtime +7 -delete
Monitoring with Timer
Service:
[Unit]
Description=Check Disk Space
[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-disk.sh
Timer:
[Unit]
Description=Disk Check Timer
[Timer]
OnCalendar=*:0/15
OnBootSec=5min
[Install]
WantedBy=timers.target
Best Practices
Cron
- Use absolute paths
- Redirect output/errors
- Test scripts manually
- Document in comments
- Check logs regularly
systemd Timers
- Create separate service
- Use Persistent=true if needed
- Configure RandomizationDelay
- Test with OnBootSec first
- Monitor with journalctl
Summary
In this chapter, you learned:
System cron: /etc/crontab, /etc/cron.d/, special directories
Anacron: For intermittent systems
systemd timers: Modern alternative with dependencies
Syntax: OnCalendar for flexible scheduling
Logs: journalctl for timers, /var/log for cron
Comparison: Simple cron, more powerful timers
Practice: Scheduled backup, cleanup, monitoring
Next Steps
Module 9: System Security
Module 10: Package Management
Chapter created on: December 2024