systemd timers
Schedule jobs with a .timer and a .service: OnCalendar, catching up on missed runs with Persistent=, testing expressions, and when cron is still fine.
systemd timers
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A timer is two units: a .timer that says when, and a .service, usually Type=oneshot, that says what. The timer starts the service with the same name unless Unit= says otherwise.
- OnCalendar= takes calendar expressions like `daily` or `Mon..Fri *-*-* 02:30`. `systemd-analyze calendar` normalises one and prints when it next fires.
- OnBootSec= and OnUnitActiveSec= are monotonic timers, relative to boot or the last run, which cron cannot express.
- Persistent=true stores the last run time on disk, so a job missed while the machine was off runs once at the next boot. It only affects OnCalendar= timers.
- RandomizedDelaySec= spreads a fleet's jobs over a window so a thousand servers do not all hit the same mirror at midnight.
- Over cron, a timer gives you the journal for output, `systemctl list-timers` for the schedule, and every service setting for the job: resource limits, sandboxing, dependencies.
- Enable the .timer, not the .service. The service has no [Install] section of its own.
The job that never ran
A database backup is set in cron for 03:00. The machine is a laptop used as a build box, or a VM that is shut down at night to save money. At 03:00 it is off, so cron does nothing, and nothing tells you. Weeks later someone needs a backup.
Cron does one thing: at a given minute, if it is running, it starts a command. Where the output goes, what happens if the machine was off, what limits the job runs under and whether it overlaps itself are all left to you. The cron and at lesson covers the crontab format. systemd timers answer those other questions, because the job is an ordinary service.
Two units, one job
A timer is a pair. The .service says what to run, the .timer says when. By default a timer starts the service with the same name, so backup.timer starts backup.service.
Debian’s log rotation runs from a timer. I downloaded the current logrotate package (3.22.0) from deb.debian.org and unpacked it on this Mac. Its logrotate.timer:
[Unit]
Description=Daily rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)
[Timer]
OnCalendar=daily
RandomizedDelaySec=1h
Persistent=true
[Install]
WantedBy=timers.target
And logrotate.service, trimmed:
[Unit]
Description=Rotate log files
RequiresMountsFor=/var/log
ConditionACPower=true
[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.conf
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7
PrivateTmp=true
ProtectSystem=full
The service is Type=oneshot: it runs to completion and stops. It has no [Install] section, because nothing should start it at boot. The timer has WantedBy=timers.target, so enabling the timer is what makes it run. The service also runs at the lowest CPU and I/O priority and with some sandboxing, which a crontab line cannot express without wrapper commands.
The same package still ships /etc/cron.daily/logrotate, and it begins:
# skip in favour of systemd timer
if [ -d /run/systemd/system ]; then
exit 0
fi
So on a systemd machine the cron job steps aside and the timer does the work. On a system without systemd, cron still rotates logs.
Saying when
There are two families of trigger, from systemd.timer(5).
Calendar timers, OnCalendar=, fire at wall-clock times. The syntax is DayOfWeek Year-Month-Day Hour:Minute:Second, with * for any, , for lists, .. for ranges and / for repeats. systemd.time(7) defines the shorthands:
minutely → *-*-* *:*:00
hourly → *-*-* *:00:00
daily → *-*-* 00:00:00
monthly → *-*-01 00:00:00
weekly → Mon *-*-* 00:00:00
Some examples from that man page with their normalised forms: Wed, 17:48 becomes Wed *-*-* 17:48:00, and *:2/3 becomes *-*-* *:02/3:00, meaning every three minutes starting at minute two. A trailing UTC or an IANA zone name such as Pacific/Auckland pins the timezone.
Monotonic timers fire relative to an event: OnBootSec= after boot, OnUnitActiveSec= after the service last started, OnUnitInactiveSec= after it last finished. systemd’s own systemd-tmpfiles-clean.timer, from the Debian systemd package, uses them:
[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
Fifteen minutes after boot, then a day after each run. Cron has no way to say “a day after the last run”.
Before deploying an expression, test it. systemd-analyze calendar 'Mon..Fri 02:30' prints the normalised form and the next elapse, and --iterations=5 shows the next five. I had no systemd to run it on here, so I am not showing its output.
Catching up and spreading out
Persistent=true makes systemd store the time the service last ran on disk. When the timer is activated, at boot for example, and a scheduled run was missed while the timer was inactive, the service is started immediately. That fixes the 03:00 laptop. The man page notes it only affects OnCalendar= timers.
RandomizedDelaySec=1h in the logrotate timer adds a random delay of up to an hour on each run. On a fleet of identical machines this stops every one of them rotating, or downloading updates, at the same second. AccuracySec= is related but different: it defaults to one minute and lets systemd group nearby wake-ups to save power. Set it lower only if you really need the job on the second.
Running and inspecting
These are the commands, from systemctl(1) and journalctl(1). I did not run them here:
systemctl enable --now backup.timer # enable and start the timer, not the service
systemctl list-timers --all # next and last run for every timer
systemctl start backup.service # run the job once, right now
journalctl -u backup.service # every run's output
Running the service by hand is the test. If it works under systemctl start, it will work from the timer, because it is the same unit in the same environment. With cron, “works in my shell, fails from cron” is a classic, because cron runs with a minimal environment.
Where cron is still fine
Cron is everywhere, including containers and minimal systems without systemd, and a crontab line is quicker to write than two unit files. Per-user crontabs need no root. For a server you run, timers win on logging, missed runs and limits. For a one-line personal job, cron is not wrong.
About the video
The embed is tutoriaLinux’s “Introduction to systemd timers”, January 2022, clipped to the first 11:45. Its chapters are “Why not cron?”, a crontab implementation, the same job as a systemd timer, testing and starting the trigger unit. The clip stops before an off-topic chapter on tech interview anxiety. Doing one job both ways is the strength. From the chapter list it does not cover Persistent=, RandomizedDelaySec= or systemd-analyze calendar, which the lesson does. English was judged from the title, channel and English auto-caption track, and content from the chapters, not a full watch.
What was run: downloading and unpacking Debian’s logrotate 3.22.0 and Debian 13’s systemd 257.13 packages and reading their timer and service files on macOS. Timer behaviour is from the systemd 262 man pages, checked 2026-11-28.
Where this fits
Each timer run writes its output to the journal. Reading it well is next: journald and journalctl.
Check yourself
You write backup.timer and backup.service, run `systemctl enable --now backup.service`, and nothing ever runs on schedule. What went wrong?
The timer was never enabled or started. Enable and start backup.timer. The service is started by the timer and normally has no [Install] section.
A laptop is off every night at 03:00, when a timer with OnCalendar=*-*-* 03:00 should run. What setting makes the job run when the laptop is next switched on?
Persistent=true. systemd stores when the service last ran and starts it at activation if a scheduled run was missed.
Why would a job's timer use `OnBootSec=15min` and `OnUnitActiveSec=1d` instead of OnCalendar=daily?
To run 15 minutes after boot and then once a day relative to the last run, whatever the clock time. That suits machines that are not on at a fixed hour.
How do you check what `OnCalendar=Mon..Fri 02:30` means before deploying it?
Run `systemd-analyze calendar 'Mon..Fri 02:30'`. It prints the normalised form and the next time it will elapse. `--iterations=5` shows several.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.