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”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 | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+