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
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.