Tools for Regular Operations
116
systemd timersApart from the regular cron
daemon, a cron-style
systemd feature is to use timers. A timer allows us to define, via a unit file, a job that will be executed.
We can check the ones that are already available in our system with the following code:
#systemctl list-unit-files *.timertimers.target static dnf-makecache.timer enabled fstrim.timer disabled systemd-tmpfiles-clean.timer static
Let’s see, for example, fstrim.timer, which is used on
solid-state drives (
SSDs) to perform a trim at /usr/lib/systemd/system/fstrim.timer:
[Unit]
Description=Discard
unused blocks once a weekDocumentation=man:fstrim
[Timer]
OnCalendar=weekly
AccuracySec=1h
Persistent=true
…
[Install]
WantedBy=timers.target
The preceding timer sets a weekly execution for fstrim.service, as follows:
[Unit]
Description=Discard unused blocks
[Service]
Type=oneshot
ExecStart=/usr/sbin/fstrim -av
As the fstrim -av command shows, we are only executing this once.
Learning about time synchronization with chrony and NTP
117
One of the advantages of having the
service timers as unit files, similar to the service itself, is that they can be deployed and updated via the /etc/cron.d/ files with the regular cron daemon, which is handled by systemd.
We now know a bit more about how to schedule tasks,
but to get the whole picture, scheduling always requires proper timing, so we’ll cover this next.
Share with your friends: