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
![](/var/linux_magazin/storage/images/issues/2018/214/systemd-timers/figure-1/732564-1-eng-US/Figure-1_large.png)
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.