Triggering regular tasks with Systemd
Extremely Hesitant
If the computer is not running at the selected time, Systemd cannot create a backup. In Listing 2, the Persistent=true
setting ensures that Systemd catches up with the task as quickly as possible in such situations. However, if several actions start simultaneously, they can slow down the system or even interfere with each other.
To prevent a traffic jam, Systemd randomly delays execution by a few seconds if necessary. The maximum number of seconds it can wait before executing is stated after RandomizedDelaySec=
. Systemd interprets the number as minutes for a trailing m
and as hours for an h
. In Table 1, you will find all other supported time units; you can also combine these. Systemd would delay the backup by a maximum of 90 seconds if you state RandomizedDelaySec="1m 30s"
.
Table 1
Units Used by Systemd
Unit | Long forms | Meaning | Example |
---|---|---|---|
s |
seconds, second, sec |
second |
5s |
m |
minutes, minute, min |
minute |
10m |
h |
hours, hour, hr |
hours |
2h |
d |
days, day |
day |
7d |
w |
weeks, week |
week |
2w |
M |
months, month |
month |
6M |
y |
years, year |
year |
4y |
Repetitions
Systemd lets you schedule a task to occur at some recurring interval without specifying an exact time – for example, every 15 minutes or once a week. Use the OnCalendar=weekly
option to start a weekly backup. In addition to weekly
, you'll find options for minutely
, hourly
, daily
, monthly
, yearly
, quarterly
, and semiannually
.
If you want to run a task 15 minutes after system startup, use the following settings instead of OnCalendar=...
:
OnBootSec=15m OnUnitActiveSec=1w
OnBootSec=
specifies how many seconds after system startup Systemd should execute the task. In the example, the timer goes off 15 minutes after the system startup. The second setting, OnUnitActiveSec=
, tells Systemd the time intervals at which it should repeat the task. In the example, Systemd would run the backup 15 minutes after system startup and then every week.
With both settings, you can use the units from the Table 1 and combine the information. For example, the OnBootSec="5m 30s"
setting would execute the task five and a half minutes after system startup.
If a timer is based on a (calendar) date, as per Listing 2, it is known as a "Calendar Timer." If, on the other hand, a timer starts after a specified period of time relative to an event, such as a system start, Systemd refers to it as a "monotonic timer." Such timers work independently of the time zone.
The timer is not only triggered shortly after system startup, but also responds to other events listed in Table 2. As in the previous example, several settings can be combined with each other; each setting must have its own line.
Table 2
Monotonic Timers
Setting | Refers to the moment when… |
---|---|
OnActiveSec= |
… the timer was activated. |
OnBootSec= |
… the computer was booted. |
OnStartupSec= |
… Systemd started. |
OnUnitActiveSec= |
… the unit that activates the timer was last activated. |
OnUnitInactiveSec= |
… the unit that activates the timer was last deactivated. |
Relationship Helper
The systemd-analyze
tool helps you figure out the correct times. If you pass it the calendar
parameter, systemd-analyze
converts the relative time specifications into other formats (Figure 1). The following command tells you, for example, which day of the week weekly
corresponds to:
$ systemd-analyze calendar weekly
By default, Systemd guarantees one-minute timer accuracy. You can therefore expect the backup not to start punctually at 6:00 pm, but at 6:01 pm. If you need greater accuracy, add the line AccuracySec=30s
to the [Timer]
section. The time specification determines the desired accuracy; in the example, the action would be no later than 30 seconds after the assigned date. For such time entries, you can again use the units from Table 1.
Timers also let you wake up the computer from suspend mode on a time-controlled basis. To do this, add the line WakeSystem=true
to the [Timer]
section. Systemd only wakes the system when it is in sleep mode and if the hardware and the BIOS/UEFI of the computer support the process. Systemd is currently unable to put the computer to sleep on a time-controlled basis.
Systemd assigns the timer unit to the appropriate service unit based on the filenames. In the example, the timer backup.timer
automatically starts the command from the service unit backup.service
. Alternatively, in the [Timer]
section, you can explicitly specify the name of the service unit that you want Systemd to execute using the Unit=
setting. This is especially useful if you want to start an existing service unit with a new timer.
« 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
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
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.