Systemd Timers
Chapter 82: systemd Timers
Section titled “Chapter 82: systemd Timers”Comprehensive Guide to Scheduled Tasks with systemd
Section titled “Comprehensive Guide to Scheduled Tasks with systemd”Why This Matters in DevOps/SRE
Section titled “Why This Matters in DevOps/SRE”systemd timers are the modern replacement for cron in Linux systems:
- Modern Systems: New Linux systems use systemd timers instead of cron
- Reliability: Timers can persist missed jobs, handle system sleep
- Logging: Better integration with journald for log management
- On-Call: You’ll manage scheduled jobs and investigate missed executions
- Certification: RHCSA and RHCE cover systemd timers
Most production servers now use systemd timers for scheduled tasks.
82.1 Understanding systemd Timers
Section titled “82.1 Understanding systemd Timers”Timer Overview
Section titled “Timer Overview” systemd Timers+------------------------------------------------------------------+| || What are timers? || +----------------------------------------------------------+ || | • Units that trigger service activation at specific times | || | • Modern replacement for cron | || | • More precise and flexible | || | • Survive reboots with Persistent=true | || +----------------------------------------------------------+ || || Components: || +----------------------------------------------------------+ || | Timer unit (.timer) - defines schedule | || | Service unit (.service) - defines what to run | || +----------------------------------------------------------+ || || Advantages over cron: || +----------------------------------------------------------+ || | • Dependencies on other services | || | • Can be triggered by events | || | • Better logging via journal | || | • Can run missed jobs on boot | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+82.2 Timer Configuration
Section titled “82.2 Timer Configuration”Timer Unit File
Section titled “Timer Unit File”[Unit]Description=My Scheduled TimerRequires=myservice.service
[Timer]# Time-based triggersOnCalendar=daily # Every day at midnightOnCalendar=Mon *-*-* 02:00:00 # Every Monday at 2 AMOnCalendar=*:0/15 # Every 15 minutesOnCalendar=*-*-01 00:00:00 # First day of month
# Time from eventsOnBootSec=5min # 5 minutes after bootOnActiveSec=1h # 1 hour after activationOnUnitActiveSec=1h # 1 hour after service last ran
# OptionsPersistent=true # Run missed jobs on bootRandomizedDelaySec=1h # Random delay to prevent stormsWakeSystem=true # Wake system from suspendAccuracySec=1us # Accuracy (default: 1min)Unit=myservice.service # Default service to trigger
[Install]WantedBy=timers.targetCalendar Time Format
Section titled “Calendar Time Format” Calendar Format+------------------------------------------------------------------+| || Examples: || +----------------------------------------------------------+ || | daily | Every day at midnight | || | hourly | Every hour | || | weekly | Every Monday at midnight | || | monthly | First day of month | || | *-*-01 00:00:00| 1st of month at midnight | || | Mon *-*-* 02:00 | Every Monday at 2 AM | || | *-*-* *:0/15 | Every 15 minutes | || | *-*-* 12:00:00 | Every day at noon | || | 2024-*-* 00:00 | Daily after Jan 1, 2024 | || | *:0/5:0 | Every 5 seconds | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+82.3 Service Unit
Section titled “82.3 Service Unit”[Unit]Description=My Scheduled Service
[Service]Type=oneshot # Run once and exitExecStart=/usr/local/bin/myscript.shUser=rootStandardOutput=journalStandardError=journal
# Or for long-running services:# Type=simple# RemainAfterExit=no82.4 Management Commands
Section titled “82.4 Management Commands”# List all timerssystemctl list-timerssystemctl list-timers --all
# Start/stop timersudo systemctl start mytimer.timersudo systemctl stop mytimer.timer
# Enable/disable timersudo systemctl enable mytimer.timersudo systemctl disable mytimer.timer
# Check timer statussystemctl status mytimer.timer
# View next run timessystemctl list-timers | grep mytimer
# Manual trigger (test)sudo systemctl start myservice.service
# View related logsjournalctl -u myservice.servicejournalctl -u mytimer.timer -f82.5 Examples
Section titled “82.5 Examples”Daily Backup Timer
Section titled “Daily Backup Timer”[Unit]Description=Daily Backup TimerRequires=daily-backup.service
[Timer]OnCalendar=dailyPersistent=trueRandomizedDelaySec=30m
[Install]WantedBy=timers.targetWeekly Cleanup Timer
Section titled “Weekly Cleanup Timer”[Unit]Description=Weekly CleanupRequires=weekly-cleanup.service
[Timer]OnCalendar=Sun 3:00Persistent=true
[Install]WantedBy=timers.targetHourly Health Check
Section titled “Hourly Health Check”[Unit]Description=Hourly Health CheckRequires=health-check.service
[Timer]OnCalendar=*:0/60 # Every hourWakeSystem=true
[Install]WantedBy=timers.target82.6 Interview Questions
Section titled “82.6 Interview Questions”Basic Questions
Section titled “Basic Questions”-
What are systemd timers?
- Units for scheduling tasks, modern replacement for cron
-
What does Persistent=true do?
- Runs missed jobs after reboot
-
What’s the difference between cron and systemd timers?
- Timers have better logging, dependencies, and persist across reboots
-
What is OnCalendar?
- Calendar-based scheduling syntax
Summary
Section titled “Summary” Quick Reference+------------------------------------------------------------------+| || Commands: || +----------------------------------------------------------+ || | systemctl list-timers | List timers | || | systemctl start timer | Start timer | || | systemctl enable timer | Enable at boot | || +----------------------------------------------------------+ || || Key Options: || +----------------------------------------------------------+ || | OnCalendar | Calendar schedule | || | Persistent=true | Run missed jobs | || | RandomizedDelaySec | Random delay | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+Common Mistakes & Anti-Patterns
Section titled “Common Mistakes & Anti-Patterns”1. Not Enabling Timers
Section titled “1. Not Enabling Timers”WRONG:
# Only start, not enablesudo systemctl start backup.timer# Timer won't start after rebootCORRECT:
# Enable to start at bootsudo systemctl enable --now backup.timerWhy: Without enable, timer doesn’t survive reboot.
2. Wrong OnCalendar Syntax
Section titled “2. Wrong OnCalendar Syntax”WRONG:
OnCalendar=daily# Too vague - runs at midnight every dayCORRECT:
OnCalendar=*-*-02 04:30:00# Runs at 4:30 AM on 2nd of each monthWhy: Specific schedules prevent confusion and conflicts.
Interview Questions
Section titled “Interview Questions”Conceptual Questions
Section titled “Conceptual Questions”-
Q: What’s the advantage of systemd timers over cron?
- A: Timers persist across system sleep/hibernate, better logging via journald, dependencies can control when jobs run, can delay randomly to prevent thundering herd.
-
Q: How do you debug a timer that isn’t firing?
- A: Check
systemctl status timer, checkjournalctl -u service, verify timer is enabled, check next activation time withsystemctl list-timers.
- A: Check
End of Chapter 82: systemd Timers