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
-
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.