Skip to main content
RH134

RHCSA 8 – Systemd Timer Units

By December 14, 2019September 12th, 2022No Comments

RHCSA 8 Study Guide

A new method of scheduling tasks that is part of the systemd eco-system are systemd timer units. These activate service units on specific intervals. The documentation for these timer units can be read using the man pages:

# man 5 systemd.timer

We can list currently installer timer units, we should know that we can use systemctl for this:

# systemctl list-unit-files --type=timer

 

We would expect to have the dnf/yum makecache timer. This ensures that the metadata for the repositories is kept up-to-date. It will run 10 minutes after boot and then hourly after this. An easy way to quickly check the age of the metadata is to run a check-update with yum. This does not do anything other than list available updates but does show the metadata age:

# yum check-update | head -n1
Last metadata expiration check: 0:02:15 ago on Wed 27 Nov 2019 14:36:51 GMT.

Here we can see the metadata was updated 2 minutes before the command was run. So, how does this work. The easiest way is to dice straight into the timer unit. Using systemctl cat we can list the contents of the unit file:

# systemctl cat dnf-makecache.timer
# /usr/lib/systemd/system/dnf-makecache.timer
[Unit]
Description=dnf makecache --timer
ConditionKernelCommandLine=!rd.live.image
# See comment in dnf-makecache.service
ConditionPathExists=!/run/ostree-booted
Wants=network-online.target

[Timer]
OnBootSec=10min
OnUnitInactiveSec=1h
Unit=dnf-makecache.service

[Install]
WantedBy=multi-user.target

The timer OnBootSec=10min runs the unit file 10 minutes after booting. The OnUnitInactive=1h means that it will run again 1 hour after the unit file becomes inactive. If the unit file runs at 10:00 and takes 2 minutes to run; it will run again at 11:02. The execution of the actual command is handled by the Unit=dnf-makecache.service entry. This unit file is disabled but runs when called by the timer unit. We can check when it last run through the status:

# systemctl status dnf-makecache.service  | grep Active
   Active: inactive (dead) since Wed 2019-11-27 14:36:51 GMT; 11min ago

The timer should execute again on my system at 15:36. However a more simple way of checking when a timer last ran and will run again is provided with the sub-command list-timers:

$ systemctl list-timers 
NEXT                         LEFT       LAST                         PASSED    UNIT                  >

Thu 2019-12-12 16:32:14 UTC  20min left Wed 2019-12-11 16:32:14 UTC  23h ago   systemd-tmpfiles-clean>

Thu 2019-12-12 16:41:22 UTC  29min left Thu 2019-12-12 15:41:22 UTC  30min ago dnf-makecache.timer   >

Fri 2019-12-13 00:00:00 UTC  7h left    Thu 2019-12-12 00:00:01 UTC  16h ago   unbound-anchor.timer

Installing new package that make use of systemd timers will add to the list of enabled timers. For example, the sysstat package used to make use of crond to schedule its tasks now it uses systemd timer units.

# yum install sysstat
# systemctl cat sysstat-collect.timer
[Unit]
Description=Run system activity accounting tool every 10 minutes
[Timer]
OnCalendar=*:00/10

[Install]
WantedBy=sysstat.service

The OnCalendar timer will run every hour starting on the hour and then every 10 minutes and is used to gather system performance data. Expect to see more on these systemd timer units as they are here to stay.

Online Instructor Led Classes from TheUrbanPenguin