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)