10.3. Scheduling with Systemd Timers

A systemd timer is a unit whose job is to make systemd start another unit at chosen times. Scheduling becomes a property of the service definition itself: the job runs with systemd's process supervision, its output lands in the journal, systemctl reports the schedule and the last run, and everything from resource limits to failure handling that Chapter 8 and Chapter 9 built for services applies unchanged. The examples in this section run on clock, as john unless a block shows sudo.

10.3.1. Calendar and Monotonic Schedules

Timer schedules come in two families. Calendar schedules name wall-clock times - "09:00 every weekday" - in a normalized format that systemd-analyze calendar both validates and dry-runs without scheduling anything:

$ systemd-analyze calendar "Mon..Fri *-*-* 09:00:00"
Normalized form: Mon..Fri *-*-* 09:00:00
    Next elapse: Fri 2026-11-20 09:00:00 UTC
       From now: 9h left

The tool prints the normalized form, the next time it would elapse, and how far away that is. --iterations shows a repeated pattern:

$ systemd-analyze calendar --iterations=3 "*-*-* *:00:00"
Normalized form: *-*-* *:00:00
    Next elapse: Fri 2026-11-20 00:00:00 UTC
       From now: 4min 8s left
   Iteration #2: Fri 2026-11-20 01:00:00 UTC
       From now: 1h 4min left
   Iteration #3: Fri 2026-11-20 02:00:00 UTC
       From now: 2h 4min left

Common aliases normalize to plain calendar forms:

$ systemd-analyze calendar "daily"
  Original form: daily
Normalized form: *-*-* 00:00:00
    Next elapse: Fri 2026-11-20 00:00:00 UTC
       From now: 4min 6s left
Calendar form Meaning
*-*-* *:*:00 Every minute (used by the examples below).
*-*-* *:00:00 Every hour on the hour.
Mon..Fri *-*-* 09:00:00 09:00 on Monday through Friday.
daily (normalizes to *-*-* 00:00:00) Midnight daily; weekly, monthly, quarterly, and yearly follow the same pattern.

The grammar is day-of-week, year-month-day, hour:minute:second, with * wildcards, lists (09,12,18), ranges (09..17), and repetition steps (0/15 for every quarter hour); systemd.time(7) is the complete reference. systemd-analyze timespan translates human durations the same way - useful when a directive's unit is microseconds and you think in minutes:

$ systemd-analyze timespan "1h 30min"
Original: 1h 30min
      μs: 5400000000
   Human: 1h 30min

Monotonic schedules are the second family: they name durations relative to a fixed event, not to the wall clock. OnBootSec= counts from boot, OnStartupSec= from systemd's start, OnActiveSec= from the timer's own activation, and OnUnitActiveSec= from the last activation of the unit the timer starts. A timer with OnBootSec=10min runs its job ten minutes after every boot, whenever that lands on the clock; Section 10.3.3 uses the OnActiveSec= / OnUnitActiveSec= pair to build a fast repeating timer. The two families mix freely in one timer, and one timer may carry several schedules of either kind.

10.3.2. Service and Timer Unit Pairs

Build a minimal working pair: a script, a oneshot service that runs it as john, and a timer that starts the service every minute. The workspace on clock, created once for the chapter's scheduling files:

$ mkdir -p ~/debian-handbook/timekeeping

Silent. Make it the working directory as well, so the relative filenames used from here to the end of the chapter resolve to it:

$ cd ~/debian-handbook/timekeeping

Also silent; the following files live there. First the script, saved as heartbeat.sh:

#!/bin/bash
printf '%s heartbeat\n' "$(date -u +%FT%TZ)" >> /home/john/debian-handbook/timekeeping/heartbeat-runs.log

It appends one UTC-stamped line to heartbeat-runs.log and prints nothing. The service unit, handbook-heartbeat.service - a oneshot, run as john, with the workspace as its working directory:

[Unit]
Description=Handbook timer heartbeat

[Service]
Type=oneshot
User=john
Group=john
WorkingDirectory=/home/john/debian-handbook/timekeeping
ExecStart=/home/john/debian-handbook/timekeeping/heartbeat.sh

And the timer unit, handbook-heartbeat.timer:

[Unit]
Description=Run handbook heartbeat every minute

[Timer]
OnCalendar=*-*-* *:*:00
AccuracySec=1s
RandomizedDelaySec=5s
Persistent=true
Unit=handbook-heartbeat.service

[Install]
WantedBy=timers.target

The [Timer] directives are the substance:

Timer directive Description
OnCalendar= Wall-clock schedule; the form validated in Section 10.3.1.
OnBootSec= / OnActiveSec= / OnUnitActiveSec= Monotonic schedules relative to boot, the timer's activation, or the job's last activation.
AccuracySec= Coalescing window around the elapse time (default 1min); smaller is more exact, larger lets systemd batch wakeups.
RandomizedDelaySec= Random delay added to each elapse within the window, to spread fleet load.
Persistent= Run one catch-up activation if the last scheduled elapse was missed (calendar schedules only).
Unit= The unit to start; defaults to the timer's own name with .service.

WantedBy=timers.target in [Install] is what makes systemctl enable work - enabled timers are collected under a dedicated target, which is also what systemctl list-timers lists. Install the two units, reload, and validate exactly as Chapter 8 practiced:

$ sudo install -m 644 handbook-heartbeat.service /etc/systemd/system/handbook-heartbeat.service
$ sudo install -m 644 handbook-heartbeat.timer /etc/systemd/system/handbook-heartbeat.timer
$ sudo systemctl daemon-reload

Each prints nothing; the daemon-reload re-reads unit files from disk. Syntax validation:

$ sudo systemd-analyze verify /etc/systemd/system/handbook-heartbeat.timer

Also silent - verify prints problems only, and a zero exit status means none were found. Enable the timer and start it now:

$ sudo systemctl enable --now handbook-heartbeat.timer
Created symlink '/etc/systemd/system/timers.target.wants/handbook-heartbeat.timer' → '/etc/systemd/system/handbook-heartbeat.timer'.

The enable half printed the symlink it created under timers.target.wants; the --now half started the timer. See the schedule systemd computed:

$ systemctl list-timers handbook-heartbeat.timer
NEXT                        LEFT LAST PASSED UNIT                     ACTIVATES
Thu 2026-11-19 23:57:04 UTC  10s -         - handbook-heartbeat.timer handbook-heartbeat.service

1 timers listed.
Pass --all to see loaded but inactive timers, too.

The next elapse is 23:57:04 - the coming minute boundary at :00 plus a randomized delay of up to five seconds, drawn here as four. LAST is empty because nothing has run yet. The timer's own view of its properties:

$ systemctl show handbook-heartbeat.timer -p NextElapseUSecRealtime -p LastTriggerUSec -p RandomizedDelayUSec -p Persistent -p AccuracyUSec -p Unit
Unit=handbook-heartbeat.service
NextElapseUSecRealtime=Thu 2026-11-19 23:57:04 UTC
LastTriggerUSec=
AccuracyUSec=1s
RandomizedDelayUSec=5s
Persistent=yes

systemctl show prints the requested properties in its own order, not the order of the -p options. LastTriggerUSec will hold the last activation once one has happened. Wait for the minute to pass, then verify the run the three ways an administrator actually uses - the script's own log, the journal, and the timer list:

$ cat heartbeat-runs.log
2026-11-19T23:57:04Z heartbeat

One line, stamped 23:57:04 - matching NEXT exactly. The journal for the service:

$ sudo journalctl -u handbook-heartbeat.service --no-pager -n 5
Nov 19 23:57:04 clock systemd[1]: Starting handbook-heartbeat.service - Handbook timer heartbeat...
Nov 19 23:57:04 clock systemd[1]: handbook-heartbeat.service: Deactivated successfully.
Nov 19 23:57:04 clock systemd[1]: Finished handbook-heartbeat.service - Handbook timer heartbeat.

Three lines per run - started, deactivated, finished - the oneshot lifecycle from Chapter 8, timestamped by the journal rather than by the script. A systemctl list-timers handbook-heartbeat.timer after the run would now show LAST populated with 23:57:04 and a new NEXT one minute later.

10.3.3. Missed Runs, Randomized Delays, and Overlapping Work

Persistent=true answers the question a machine that is not always powered asks: what if the system was off at the scheduled time? systemd records each calendar timer's last trigger in a stamp file - /var/lib/systemd/timers/stamp-<timer-name> - and on timer startup compares it against the schedule; if an elapse was missed, the service runs once immediately, and the stamp is updated. Simulate a missed run without powering anything off, by aging the stamp two hours into the past:

$ sudo systemctl stop handbook-heartbeat.timer
$ sudo touch -d "2 hours ago" /var/lib/systemd/timers/stamp-handbook-heartbeat.timer

Both silent. Start the timer again - silent as well - and look a few seconds later:

$ sudo systemctl start handbook-heartbeat.timer
$ systemctl list-timers handbook-heartbeat.timer
NEXT                        LEFT LAST                        PASSED UNIT                     ACTIVATES
Thu 2026-11-19 23:58:01 UTC  46s Thu 2026-11-19 23:57:11 UTC 3s ago handbook-heartbeat.timer handbook-heartbeat.service

1 timers listed.
Pass --all to see loaded but inactive timers, too.

LAST is three seconds ago - systemd treated the aged stamp as evidence of a missed elapse and ran the service immediately on timer start, exactly as it would after a power-off spanning the scheduled time. The journal confirms a second, off-schedule run:

$ sudo journalctl -u handbook-heartbeat.service --no-pager -n 6
Nov 19 23:57:04 clock systemd[1]: Starting handbook-heartbeat.service - Handbook timer heartbeat...
Nov 19 23:57:04 clock systemd[1]: handbook-heartbeat.service: Deactivated successfully.
Nov 19 23:57:04 clock systemd[1]: Finished handbook-heartbeat.service - Handbook timer heartbeat.
Nov 19 23:57:11 clock systemd[1]: Starting handbook-heartbeat.service - Handbook timer heartbeat...
Nov 19 23:57:11 clock systemd[1]: handbook-heartbeat.service: Deactivated successfully.
Nov 19 23:57:11 clock systemd[1]: Finished handbook-heartbeat.service - Handbook timer heartbeat.

The same mechanism serves real downtime: a laptop suspended overnight with a Persistent=true daily timer runs the job once at resume. It is one catch-up run, not a replay of every missed elapse - the design guarantee is "the work happened", not "the work happened n times".

RandomizedDelaySec was visible in every NEXT above: the nominal minute boundary plus up to five seconds, drawn per elapse and applied before the AccuracySec coalescing window. It exists so that a fleet of identically configured machines does not hit one server at the same second - the same reason Debian's own apt-daily.timer randomizes its refresh window (Chapter 6 showed its output). For work that must fire at an exact second, set RandomizedDelaySec=0 and AccuracySec=1s, as both practice timers here do.

Overlapping work is the question the remaining experiment answers: what happens when the job takes longer than the interval between runs? Make a six-second job on a two-second timer. The script, slow.sh:

#!/bin/bash
printf '%s START pid=%s\n' "$(date -u +%FT%TZ)" "$$" >> /home/john/debian-handbook/timekeeping/slow-runs.log
sleep 6
printf '%s END pid=%s\n' "$(date -u +%FT%TZ)" "$$" >> /home/john/debian-handbook/timekeeping/slow-runs.log

It logs its start and end with the process ID, so overlaps - or their absence - are visible in the log. The service handbook-slow.service, oneshot as john like the heartbeat:

[Unit]
Description=Handbook slow timer job

[Service]
Type=oneshot
User=john
Group=john
WorkingDirectory=/home/john/debian-handbook/timekeeping
ExecStart=/home/john/debian-handbook/timekeeping/slow.sh

The timer handbook-slow.timer uses the monotonic family: first run one second after the timer starts, then every two seconds counted from the service's last activation:

[Unit]
Description=Run handbook slow job every two seconds

[Timer]
OnActiveSec=1s
OnUnitActiveSec=2s
AccuracySec=100ms
Unit=handbook-slow.service

[Install]
WantedBy=timers.target

OnUnitActiveSec measures from the last activation of the service, so the two-second demand stacks up while the six-second job is still running. Install and start:

$ sudo install -m 644 handbook-slow.service /etc/systemd/system/handbook-slow.service
$ sudo install -m 644 handbook-slow.timer /etc/systemd/system/handbook-slow.timer
$ sudo systemctl daemon-reload
$ sudo systemctl start handbook-slow.timer

All four silent. Let it run for about twenty seconds, then stop the timer - silent too - to freeze the record:

$ sudo systemctl stop handbook-slow.timer
$ cat slow-runs.log
2026-11-19T23:57:20Z START pid=32369
2026-11-19T23:57:26Z END pid=32369
2026-11-19T23:57:26Z START pid=32374
2026-11-19T23:57:32Z END pid=32374
2026-11-19T23:57:32Z START pid=32387
2026-11-19T23:57:38Z END pid=32387

Each run is six seconds, each starts exactly when the previous one ended, and the PIDs differ - three separate processes, never two at once. Figure 10.3 plots the log against the two-second demand.

Timer serialization

Figure 10.3. The measured run log: the two-second OnUnitActiveSec demand (dashed ticks) accumulates during each six-second run, and the next run starts at the moment the previous one ends - serialized, not overlapping. Data from the example system's slow-runs.log, 2026-11-19 23:57 UTC.

The journal agrees - Starting never appears before the previous Finished:

$ sudo journalctl -u handbook-slow.service --no-pager -n 12
Nov 19 23:57:20 clock systemd[1]: Starting handbook-slow.service - Handbook slow timer job...
Nov 19 23:57:26 clock systemd[1]: handbook-slow.service: Deactivated successfully.
Nov 19 23:57:26 clock systemd[1]: Finished handbook-slow.service - Handbook slow timer job.
Nov 19 23:57:26 clock systemd[1]: Starting handbook-slow.service - Handbook slow timer job...
Nov 19 23:57:32 clock systemd[1]: handbook-slow.service: Deactivated successfully.
Nov 19 23:57:32 clock systemd[1]: Finished handbook-slow.service - Handbook slow timer job.
Nov 19 23:57:32 clock systemd[1]: Starting handbook-slow.service - Handbook slow timer job...
Nov 19 23:57:38 clock systemd[1]: handbook-slow.service: Deactivated successfully.
Nov 19 23:57:38 clock systemd[1]: Finished handbook-slow.service - Handbook slow timer job.

This is the general behavior: a timer does not start a second instance of a service that is still active. A long-running job on a short interval therefore degrades to back-to-back runs, restarting immediately when each ends - the DeferReactivation= timer option controls exactly this immediate restart and defaults to off. Two design consequences follow. First, put the interval on the job's real cadence, not on a faster "just in case": there is no concurrency to prevent, but there is also no pause. Second, if a job must never take longer than its interval, measure it - the serialization here is observable precisely because the script logged its own start and end.

10.3.4. Inspecting Upcoming Runs and Execution Logs

Everything needed to audit a timer is on display in the three views used above, so this subsection assembles the toolkit:

  • systemctl list-timers - optionally with a unit name, or --all to include inactive timers. The NEXT, LEFT, LAST, and PASSED columns answer when it runs and when it last ran; ACTIVATES names the service, which is what the journal will be filtered by.
  • systemctl show <timer> -p NextElapseUSecRealtime -p LastTriggerUSec -p Persistent -p RandomizedDelayUSec -p AccuracyUSec -p Unit - machine-readable schedule facts, including the stamp-backed catch-up state.
  • journalctl -u <service> with Chapter 8's time filters (--since, --until, -b) - the execution record; each oneshot run leaves its Starting/Finished pair with journal timestamps, which is usually more trustworthy than a job's own printed clock, since the journal's timestamps come from the system clock at write time.
  • The stamp file /var/lib/systemd/timers/stamp-<timer> - readable evidence of what Persistent=true last caught up to.

One habit is worth forming: verify a timer change the same minute you make it. systemctl list-timers immediately after daemon-reload and a timer restart shows whether the new schedule took effect, and one elapsed run in the journal proves the unit pairing is right - a typo in Unit= produces a timer that elapses and a service that never appears, which the journal exposes at once.

When the practice units are no longer wanted, remove them the way they were created, bounded to the files this section installed: sudo systemctl disable --now handbook-heartbeat.timer handbook-slow.timer (which reports the removed timers.target.wants symlinks), delete the four unit files from /etc/systemd/system, then sudo systemctl daemon-reload and sudo systemctl reset-failed for the two services. The scripts and logs stay in ~/debian-handbook/timekeeping until the chapter's closing cleanup.