Linux Labs

Packages, services, and logs / Stage 11 of 13

Cron jobs and systemd timers

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 8 · theory · 5 min

systemd timers

systemd has schedules of its own: timers. They start services and write everything to the journal.

A timer is a unit whose name ends in .timer. It sets the moment to start, and the service of the same name, .service, describes what to do. OnCalendar=daily is every day at midnight, OnActiveSec=5s five seconds after the timer starts. systemd-analyze calendar 'Mon *-*-* 02:15' shows how systemd reads a calendar expression: the next trigger is the coming Monday at 02:15.

A timer is enabled just like a service at the start of the services module: enable starts it at boot, start right now, enable --now both. systemctl list-timers shows each timer's next run (NEXT) and last run (LAST). The service a timer starts is usually short: it does the work and exits, and it stays inactive until the next trigger. Unlike with cron, its output goes to the service's journal.

Check yourself 1 question

lab-report.timer is active (waiting), while lab-report.service is inactive (dead) with Result=success. What does that mean?

  • Result=success means the service's last run succeeded. inactive means it is not running right now: it did its work and exited.

  • The timer starts the service, and in such a pair it is the timer you enable. A service without its own [Install] cannot be enabled anyway: systemctl is-enabled says static.

A timer's short service does its work and exits, so it is inactive between triggers. systemctl list-timers shows the next run in the NEXT column.

Key points

  • A .timer sets the moment, a .service does the work.
  • enable turns the timer on at boot, start right now; systemctl list-timers shows NEXT and LAST.
  • Between triggers the timer's service is inactive, and that is normal.
Ubuntu 24.04
Loading…