6 Working With Systemd Timers
Use timer unit files in systemd
to schedule tasks, in a similar way to
the cron
utility that uses crontab
and other
cron
jobs for the same purpose.
Packages that use specific services to function in the system include their own
systemd
timer unit files. When those packages are
installed on Oracle Linux
8, the timer unit files are automatically included. To
display active timer unit files, run the following command:
systemctl list-unit-files --type=timer
Note:
The list of timer files differs depending on where Oracle Linux 8 is running, such as in an Oracle Cloud Infrastructure instance, a physical system, and so on.
Each timer unit file contains parameter settings that manage the schedule of a task. For
example, the schedule for running dnf-makecache.service
is
set in the dnf-makecache.timer
file. To review the contents
of that file, run the following command:
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
RandomizedDelaySec=60m
Unit=dnf-makecache.service
[Install]
WantedBy=timers.target
The schedule information is specified under the [Timer]
section. In the
sample configuration, the dnf-makecache.service
service is set to
automatically run 10 minutes after the system is booted. The service then goes into idle
mode for an hour, as specified by the OnUnitInactiveSec
parameter. At
the end of the hour, the service runs again. This cycle continues every hour
indefinitely.
The RandomizedDelaySec
setting provides a value limit for how much a
run can be delayed beyond its schedule. In the example, the service
is allowed to run one minute later than its schedule at the latest.
This parameter is useful for preventing too many jobs that start at
the same time on a specified schedule, which would otherwise risk
overloading the resources.
OnCalendar
is another useful parameter for task scheduling. Suppose that the
parameter is set as follows:
OnCalendar=*:00/10
The *:00
indicates every hour at the top of the hour, while the
/10
setting indicates 10 minutes. Therefore, the job is set to run
hourly, at ten minutes past the top of the hour.
For a complete list of systemd
timer unit file parameters for scheduling
a job, see the systemd.timer(5)
manual pages.
For more information about using systemd
with Oracle Linux
8 systems, see Oracle Linux 8: Managing the System With
systemd.