Triggering regular tasks with Systemd
Winding Up the Clock
If you want Systemd to activate the timer directly at system startup, you need an [Install]
section in the timer unit. The WantedBy=
setting tells which other units the timer should start with. In Listing 2, the WantedBy=timers.target
setting ensures that Systemd starts the timer together with all other timers at the regular system startup time.
If you want Systemd to start the timer at startup time, you have to enable it explicitly (Listing 4, first line). Alternatively, you can start the timer manually (second line). All currently configured timers are listed by the systemctl list-timers
command (Figure 2).
Listing 4
Enabling at Startup
$ systemctl enable backup.timer $ systemctl start backup.timer
In the table under Next
, you can read when the system timer will execute the task the next time. The time remaining until then is in the Left
column. Similarly, you can see under Load
when systemd-timer
last executed the task. How long ago that was is shown in the Passed
column. Under Unit
, you will find the name of the corresponding timer and thus its configuration file.
You can end the display by pressing [Q]. By default, Systemctl only presents timers that are currently enabled. You can display the inactive timers on screen by appending the --all
parameter.
Snooze Button
If required, each timer can be stopped manually (Listing 5, first line) and disabled (second line). The manpage [1], which goes by the name of systemd.timer
, provides explanations for all presented settings. man systemd.time
provides further information on the format of dates and times and offers numerous additional examples.
Listing 5
Manual Stop
$ sudo systemctl stop my.timer $ sudo systemctl disable my.timer
Short-Term Alarm
If you want Systemd to make a single backup in exactly 30 minutes, use systemd-run
. The command looks like the first line of Listing 6. The /usr/bin/backup.sh /mnt
command appended there is executed by Systemd at the specified time. Use the parameter --on-active
to tell it the waiting time.
Listing 6
Examples
$ systemd-run --on-active=30m /usr/bin/backup.sh /mnt $ systemd-run --on-calendar=weekly --unit backup.service
The time units again correspond to those in Table 1. In the example, Systemd interprets the 30m
as half an hour. Alternatively, use --on-calendar=
to enter a specific date. The details are again provided in the same way as in the timer unit. With appropriate time specifications such as weekly
, the action can execute repeatedly.
In any case, systemd-run
creates a new timer in the background without you needing to create a service file (Figure 3). If a suitable service unit already exists, you can alternatively let systemd-run
launch it. To do this, simply pass in the name of the service unit using the --unit
parameter. The example from the second line of Listing 6 starts the task stored in the backup.service
service unit every week.
The timers generated by systemd-run
only exist temporarily. If you use the --on-active
parameter, the timer disappears immediately after the action has been executed; in any case, it disappears after rebooting the system. systemd-run
only creates a timer for a service unit if no suitable timer unit exists.
« Previous 1 2 3 4 Next »
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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.