Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 19System · systemd

How to create a systemd timer

A systemd timer is the modern cron: it starts a service unit on a schedule. You get things cron does not give you: every run in the journal with its exit code, runs that were missed while the server was off, a random delay so 100 servers do not start at the same second, resource limits, and systemctl list-timers to see the next run. This how-to builds a backup timer step by step, and shows user timers and how to test the schedule.

  • 5 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

19.1

The quick answer

Two files — the service (what to run) and the timer (when):

/etc/systemd/system/backup.service
[Unit]
Description=Nightly backup of /etc
 
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
/etc/systemd/system/backup.timer
[Unit]
Description=Run backup.service every night
 
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=10m
 
[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now backup.timer
systemctl list-timers backup.timer

19.2

Step by step

  1. Write and test the script by hand (full paths, exit code not 0 on errors — set -euo pipefail). Then test the service once, without the timer:
systemctl daemon-reload
systemctl start backup.service
systemctl status backup.service --no-pager
journalctl -u backup.service -n 20 --no-pager
  1. Choose the schedule. Two kinds:
SettingMeans
OnCalendar=dailyEvery day at 00:00.
OnCalendar=*-*-* 02:30:00Every day at 02:30.
OnCalendar=Mon..Fri 06:00Weekdays at 06:00.
OnCalendar=*:0/15Every 15 minutes.
OnCalendar=*-*-01 03:00The 1st of every month at 03:00.
OnBootSec=5min5 minutes after boot.
OnUnitActiveSec=1h1 hour after the last run started.
  1. Test the calendar line before you use it:
systemd-analyze calendar --iterations=3 'Mon..Fri 06:00'
  Original form: Mon..Fri 06:00
Normalized form: Mon..Fri *-*-* 06:00:00
    Next elapse: Tue 2026-11-17 06:00:00 UTC
       Iter. #2: Wed 2026-11-18 06:00:00 UTC
       Iter. #3: Thu 2026-11-19 06:00:00 UTC
  1. Useful extras:
  • Persistent=true — if the server was off at 02:30, the job runs at the next boot.
  • RandomizedDelaySec=10m — spreads the start over 10 minutes (many servers, one backup target).
  • In the service: Nice=10, IOSchedulingClass=idle, MemoryMax=1G, TimeoutStartSec=2h, User=backup (do not run as root if not needed).
  1. A timer for your own user (no root):
mkdir -p ~/.config/systemd/user
systemctl --user daemon-reload
systemctl --user enable --now report.timer
loginctl enable-linger alice

enable-linger lets the user's timers run when the user is not logged in.

19.3

Check that it worked

systemctl list-timers --all | grep backup
Tue 2026-11-18 02:34:12 UTC 15h left  Mon 2026-11-17 02:31:40 UTC 8h ago  backup.timer  backup.service
journalctl -u backup.service --since today

You see the next and the last run, and the journal shows the run with Finished (or Failed with the exit code).

19.4

Common problems

ProblemCauseFix
Unit backup.timer not foundNo daemon-reload after you made the files.systemctl daemon-reload.
Timer is active, the job never runsThe timer was started but not enabled, or the calendar is wrong.systemctl enable --now NAME.timer; systemd-analyze calendar.
Job runs at the wrong hourThe timer uses the server time zone.timedatectl; or add a zone: OnCalendar=*-*-* 02:30 Europe/Amsterdam.
The service "fails" but the work was doneThe script exits with a code that is not 0.Fix the exit code; read journalctl -u NAME.service.
A long job is killedType=oneshot has a start timeout.TimeoutStartSec=infinity or a real value like 2h.
User timer stops after logoutNo linger.loginctl enable-linger USER.

19.5

Go further

1What does Persistent=true do in a timer?

If the server was off at the planned time, the job runs once at the next boot.

2How do you see when a timer runs next?

systemctl list-timers (next and last run of every timer).

Alle commando's in dit hoofdstuk