Triggering regular tasks with Systemd
Alarm Clock
Systemd can start timers that automatically perform tasks at specified times. The configuration files are known as timer units.
You might want to use your Linux system to automatically create a backup every evening and rotate the log files at regular intervals. In most distributions, time-controlled tasks are handled by the Cron daemon. But Systemd is an interesting alternative to Cron. Systemd controls the startup process of most distributions, and it can also trigger time-controlled and recurring tasks.
Service Providers
The first task is to tell Systemd which task to perform. To do this, you create a configuration file, the Service Unit. Listing 1 shows an example.
Listing 1
Service Unit
[Unit] Description=Create a backup of the system [Service] ExecStart=/usr/bin/backup.sh /mnt
A service unit is a text file divided into several sections. The [Service]
section is required. ExecStart=
is followed by the command to be executed by the system. In Listing 1, Systemd would simply run a script that backs up the system to the /mnt
directory. The [Unit]
section adds some metadata. In the simplest case, Description=
is followed by a description of the task.
Service Units usually tell Systemd which services to boot when the system starts. (See the article on Systemd units elsewhere in this issue.) Systemd also supports additional sections and settings. However, since the system just needs to schedule the task, these settings are not (absolutely) necessary. In particular, you can leave out the complete [Install]
section.
Save the newly created service unit to /etc/systemd/system
. The filename corresponds to the (internal) name of the service unit. It must be unique among all service units and end with .service
, as in backup.service
. Systemd can also start existing service units or service units supplied by the distribution on a time-controlled basis. In this case, simply make a note of the filename of the service file.
Tick-Tock
To avoid burning the cake to a crisp, most hobby bakers set a kitchen timer. In a similar way, you need to set a separate timer for a task you wish to assign to Systemd.
First, create a new text file in the /etc/system/system
subdirectory. The text file should have the same filename as the service unit you created earlier, but it ends with .timer
. In the example, the file would be named backup.timer
. In Systemd speak, the file with the .timer
extension is known as the timer unit. In the timer unit, you describe when the timer should "go off," at which point, Systemd will start the backup.
The structure of a timer unit is very similar to that of a service unit. As the example from Listing 2 shows, it typically consists of three sections: [Unit]
is followed by general information about the timer. In Listing 2, this information would include a Description=
that serves mainly as a reminder for the user. Make a note on why the timer exists and what actions it triggers.
Listing 2
Timer Unit
[Unit] Description=Create a daily backup of the system [Timer] OnCalendar=*-*-* 18:15:00 Persistent=true RandomizedDelaySec=2h [Install] ZWantedBy=timers.target
Current Events
In the next section, [Timer]
, you tell Systemd when to start the task. Make a note of this time after OnCalendar=
in the notation weekday year-month-day hour:minutes: seconds. The setting OnCalendar=Fr 2018-11-30 12:00:00
tells Systemd to create the backup on Friday, November 30, 2018 at noon precisely. You can omit unnecessary information, such as the day of the week or the seconds.
Normally, you will not want Systemd to run the task once only, but repeat it. To set up a repeating event, you can simply list the corresponding days, dates, and times separated by commas. In the example from the first line of Listing 3, Systemd starts the backup November 30, 2018 at 1AM and 12 Noon.
Listing 3
Date and Time
OnCalendar=2018-11-30 01,12:00:00 OnCalendar=2018-01..12-01 01,12:00:00 OnCalendar= 2018-*-01 01,12:00:00
You can also abbreviate the number ranges with two dots ..
, which means that you do not have to list all the months, for example. The entry from the second line of Listing 3, tells Systemd to take action on the first day of each month. If the statement applies to all months, you can also use the wildcard *
(line three).
The *-*-*
entry from Listing 2 tells Systemd to run the backup every day at 18:15 in every month and every year.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.