Working with systemd
timers
1 Environment #
This document applies to the following products and product versions:
SUSE Linux Enterprise Server 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3
SUSE Linux Enterprise Server for SAP Applications 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3
SUSE Linux Enterprise High Availability Extension 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3
SUSE Linux Enterprise High Performance Computing 15 SP3, 15 SP2, 15 SP1, 15 GA
SUSE Linux Enterprise Desktop 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3
SUSE Linux Enterprise Real Time 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3
2 Introduction #
From running a backup script at regular intervals to starting a specific
process as soon as the machine boots, there are plenty of tasks that require
scheduling on a Linux system. cron has long been the go-to tool for all
scheduling needs, but the switch to systemd
introduced another way to
schedule tasks: systemd
timer units.
Similar to cron, systemd
timer units provide a mechanism for scheduling
jobs on Linux. Following the instruction below, you can learn how to create
a systemd
timer.
3 Requirements #
Basic understanding of
systemd
.Root privileges.
4 Creating a systemd
timer #
The following procedure describes how to set up a timer that triggers the
helloworld.sh
shell script.
systemd
timer #Before you set up a
systemd
timer unit, you need to create asystemd
service unit that controls the shell script. To do this, open a new text file for editing and add the following service unit definition:[Unit] Description="Hello World script" [Service] ExecStart=/usr/local/bin/helloworld.sh
Save the file under the name
helloworld.service
in the directory/etc/systemd/system/
.Open a new text file for editing and add the following timer definition:
[Unit] Description="Run Hello World script" [Timer] OnBootSec=5min OnUnitActiveSec=24h Unit=helloworld.service [Install] WantedBy=multi-user.target
The
[Timer]
section in the example above specifies what service to trigger (helloworld.service
) and when to trigger it. In this case, the optionOnBootSec
specifies a monotonic timer that triggers the service five minutes after the system boot, while the optionOnUnitActiveSec
triggers the service 24 hours after the service has been activated (that is, the timer will trigger the service once a day). Finally the optionWantedBy
specifies that the timer should start when the system has reached the multi-user target.Save the timer unit file under the name
helloworld.timer
in the/etc/systemd/system/
directory. To check the correctness of the created unit files, run the following command:>
sudo
systemd-analyze verify /etc/systemd/system/helloworld.*If the command returns no output, the files have passed the verification successfully.
To start the timer, use the command
sudo systemctl start helloworld.timer
. To enable the timer on boot, run the commandsudo systemctl enable helloworld.timer
.
5 Troubleshooting #
The best way to make sure that you catch all issues beforehand, is to verify
systemd
timer units before enabling them. Running the
systemd-analyze verify
command can help you to identify
and fix potential problems.
6 Next steps #
Learn more about
systemd
7 Related topics #
systemd
conceptual overviewCreating and managing
systemd
service units