Systemd/Timers: Unterschied zwischen den Versionen

Aus wiki.archlinux.de
Tuxnix (Diskussion | Beiträge)
Die Seite wurde neu angelegt: „{{inuse|~~~}} {{Lowercase title}} Category:Daemons and system services Category:Boot process de:Systemd/Timers fr:Systemd/cron ja:Systemd/タ…“
 
Tuxnix (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
{{inuse|[[Benutzer:Tuxnix|Tuxnix]] ([[Benutzer Diskussion:Tuxnix|Diskussion]])}}
{{inuse|[[Benutzer:Tuxnix|Tuxnix]] ([[Benutzer Diskussion:Tuxnix|Diskussion]])}}
{{Lowercase title}}
[[Category:Daemons and system services]]
[[Category:Daemons and system services]]
[[Category:Boot process]]
[[Category:Boot process]]
[[de:Systemd/Timers]]
[[de:Systemd/Timers]]
[[en:Systemd/Timers]]
[[fr:Systemd/cron]]
[[fr:Systemd/cron]]
[[ja:Systemd/タイマー]]
[[ja:Systemd/タイマー]]
[[ru:Systemd/Timers]]
[[ru:Systemd/Timers]]
[[zh-hans:Systemd/Timers]]
{{righttoc}}
{{Related articles start}}
Timer bieten die Möglichkeit Aufgaben zeitlich zu steuern. Sie bestehen aus einer {{ic|.timer}} und einer {{ic|.service}} Datei.
{{Related|systemd}}
Der Timer Dienst wird von [[systemd]] gesteuert und muss mit dem systemctl Befehl aktiviert werden. Eine Alternative hierzu ist [[cron]].
{{Related|systemd/User}}
{{Related|systemd FAQ}}
{{Related|cron}}
{{Related articles end}}
Timer bieten die Möglichkeit Aufgaben zeitgerecht zu steuern. Sie bestehen aus einer {{ic|.timer}} und einer {{ic|.service}} Datei.
Der Timer Dienst wird von [[systemd]] gesteuert und müssen mit einem systemctl Befehl aktiviert werden. Alternativen sind [[cron]] und [[anacron]].


==Hinweis==
== Beispiel ==


Dies ist mein erster Archwicki Artikel. Er ist gerade erst im entstehen
=== Die {{ic|.timer}} Datei ===
Der englische Archwikiartikel dient lediglich als Vorlage.
# Datei /usr/lib/systemd/system/foo.timer
[Unit]
Description=foo timer
[Timer]
OnBootSec=1h
OnUnitInactiveSec=1d
[Install]
WantedBy=basic.target


==Beispiel==
=== Die {{ic|.service}} Datei ===
 
# Datei /usr/lib/systemd/system/foo.service
==={{ic|.timer}} Datei===
[Unit]
 
Description=foo
==={{ic|.service}} Datei===
[Service]
 
Type=oneshot
===Aktivierung===
Nice=19
 
IOSchedulingClass=2
== Timer units ==
IOSchedulingPriority=7
Timers are like other [[systemd#Writing unit files|unit configuration files]] and are loaded from the same paths but include a {{ic|[Timer]}} section. The {{ic|[Timer]}} section defines when and how the timer activates. Timers are defined as one of two types:
  Environment="ABRT_IGNORE_PYTHON=1"
 
  ExecStart=/usr/bin/foo
* '''Monotonic timers''' activate after a time span relative to a varying starting point. There are number of different monotonic timers but all have the form of: {{ic|1=On''Type''Sec=}}. {{ic|OnBootSec}} and {{ic|OnActiveSec}} are common monotonic timers.
=== Aktivierung ===
* '''Realtime timers''' (a.k.a. wallclock timers) activate on a calendar event (like cronjobs). The option {{ic|1=OnCalendar=}} is used to define them.
systemctl enable --now foo.timer
 
For a full explanation of timer options, see the {{man|5|systemd.timer}}. The argument syntax for calendar events and time spans is defined in {{man|7|systemd.time}}.
 
== Service unit ==
 
For each {{ic|.timer}} file, a matching {{ic|.service}} file exists (e.g. {{ic|foo.timer}} and {{ic|foo.service}}).  The {{ic|.timer}} file activates and controls the {{ic|.service}} file.  The {{ic|.service}} does not require an {{ic|[Install]}} section as it is the ''timer'' units that are enabled.  If necessary, it is possible to control a differently-named unit using the {{ic|1=Unit=}} option in the timer's {{ic|[Timer]}} section.


== Management ==
== Management ==
systemctl list-timers --all
Listet alle Timer auf.


To use a ''timer'' unit [[enable]] and [[start]] it like any other unit (remember to add the {{ic|.timer}} suffix).  To view all started timers, run:
== Weblinks ==
 
{{hc|$ systemctl list-timers|
NEXT                          LEFT        LAST                          PASSED    UNIT                        ACTIVATES
Thu 2014-07-10 19:37:03 CEST  11h left    Wed 2014-07-09 19:37:03 CEST  12h ago    systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Fri 2014-07-11 00:00:00 CEST  15h left    Thu 2014-07-10 00:00:13 CEST  8h ago    logrotate.timer              logrotate.service
}}
 
{{Note|
* To list all timers (including inactive), use {{ic|systemctl list-timers --all}}.
* The status of a service started by a timer will likely be inactive unless it is currently being triggered.
* If a timer gets out of sync, it may help to delete its {{ic|stamp-*}} file in {{ic|/var/lib/systemd/timers}}. These are zero length files which mark the last time each timer was run. If deleted, they will be reconstructed on the next start of their timer.}}
 
== Example ==
 
No changes to service unit files are needed to schedule them with a timer. The following example schedules {{ic|foo.service}} to be run with a corresponding timer called {{ic|foo.timer}}.
 
=== Monotonic timer ===
 
A timer which will start 15 minutes after boot and again every week while the system is running.
 
{{hc|/etc/systemd/system/foo.timer|<nowiki>
[Unit]
Description=Run foo weekly and on boot
 
[Timer]
OnBootSec=15min
OnUnitActiveSec=1w
 
[Install]
WantedBy=timers.target
</nowiki>}}
 
=== Realtime timer ===
 
A timer which starts once a week (at 12:00am on Monday). It starts once immediately if it missed the last start time (option {{ic|1=Persistent=true}}), for example due to the system being powered off:
 
{{hc|/etc/systemd/system/foo.timer|<nowiki>
[Unit]
Description=Run foo weekly
 
[Timer]
OnCalendar=weekly
Persistent=true   
[Install]
WantedBy=timers.target</nowiki>}}
 
The format controlling {{ic|OnCalendar}} events uses the following format when more specific dates and times are required: {{ic|DayOfWeek Year-Month-Day Hour:Minute:Second}}. An asterisk may be used to specify any value and commas may be used to list possible values. Two values separated by {{ic|..}} may be used to indicate a contiguous range. In this example the service is run the first four days of each month at 12:00 PM, but ''only'' if that day is also on a Monday or a Tuesday. More information is available in {{man|7|systemd.time}}.
 
OnCalendar=Mon,Tue *-*-01..04 12:00:00
 
{{Tip|Special event expressions like {{ic|daily}} and {{ic|weekly}} refer to ''specific start times'' and thus any timers sharing such calendar events will start simultaneously. Timers sharing start events can cause poor system performance if the timers' services compete for system resources.  The {{ic|RandomizedDelaySec}} option avoids this problem by randomly staggering the start time of each timer. See {{man|5|systemd.timer}}.}}
 
== Transient .timer units ==
 
One can use {{ic|systemd-run}} to create transient {{ic|.timer}} units. That is, one can set a command to run at a specified time without having a service file. For example the following command touches a file after 30 seconds:
 
# systemd-run --on-active=30 /bin/touch /tmp/foo
 
One can also specify a pre-existing service file that does not have a timer file. For example, the following starts the systemd unit named {{ic|''someunit''.service}} after 12.5 hours have elapsed:
 
# systemd-run --on-active="12h 30m" --unit ''someunit''.service
 
See {{man|1|systemd-run}} for more information and examples.
 
== As a cron replacement ==
 
Although [[cron]] is arguably the most well-known job scheduler, ''systemd'' timers can be an alternative.
 
=== Benefits ===
 
The main benefits of using timers come from each job having its own ''systemd'' service. Some of these benefits are:
 
* Jobs can be easily started independently of their timers. This simplifies debugging.
* Each job can be configured to run in a specific environment (see {{man|5|systemd.exec}}).
* Jobs can be attached to [[cgroups]].
* Jobs can be set up to depend on other ''systemd'' units.
* Jobs are logged in the ''systemd'' journal for easy debugging.
 
=== Caveats ===
 
Some things that are easy to do with cron are difficult to do with timer units alone.
 
* Complexity: to set up a timed job with ''systemd'' you create two files and run a couple {{ic|systemctl}} commands. Compare that to adding a single line to a crontab.
* Emails: there is no built-in equivalent to cron's {{ic|MAILTO}} for sending emails on job failure. See the next section for an example of setting up a similar setup using {{ic|1=OnFailure=}}.
 
=== MAILTO ===
 
You can set up systemd to send an e-mail when a unit fails. Cron sends mail to {{ic|MAILTO}} the job outputs to stdout or stderr, but many jobs are setup to only output on error. First you need two files: an executable for sending the mail and a ''.service'' for starting the executable. For this example, the executable is just a shell script using {{ic|sendmail}}:
 
{{hc|/usr/local/bin/systemd-email|<nowiki>#!/bin/bash
 
/usr/bin/sendmail -t <<ERRMAIL
To: $1
From: systemd <root@$HOSTNAME>
Subject: $2
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8
 
$(systemctl status --full "$2")
ERRMAIL</nowiki>}}
 
Whatever executable you use, it should probably take at least two arguments as this shell script does: the address to send to and the unit file to get the status of. The ''.service'' we create will pass these arguments:
 
{{hc|/etc/systemd/system/status-email-''user''@.service|2=[Unit]
Description=status email for %i to ''user''
 
[Service]
Type=oneshot
ExecStart=/usr/local/bin/systemd-email ''address'' %i
User=nobody
Group=systemd-journal}}
 
Where {{ic|''user''}} is the user being emailed and {{ic|''address''}} is that user's email address. Although the recipient is hard-coded, the unit file to report on is passed as an instance parameter, so this one service can send email for many other units. At this point you can [[start]] {{ic|status-email-''user''@dbus.service}} to verify that you can receive the emails.
 
Then simply [[systemd#Editing provided units|edit]] the service you want emails for and add {{ic|1=OnFailure=status-email-''user''@%n.service}} to the {{ic|[Unit]}} section. {{ic|%n}} passes the unit's name to the template.
 
{{note|
* If you set up SSMTP security according to [[SSMTP#Security]] the user {{ic|nobody}} will not have access to {{ic|/etc/ssmtp/ssmtp.conf}}, and the {{ic|systemctl start status-email-''user''@dbus.service}} command will fail. One solution is to use {{ic|root}} as the User in the {{ic|status-email-''user''@.service}} unit.
* If you try to use {{ic|mail -s somelogs ''address''}} in your email script, {{ic|mail}} will fork and systemd will kill the mail process when it sees your script exit. Make the mail non-forking by doing {{ic|mail -Ssendwait -s somelogs ''address''}}.}}
 
=== Using a crontab ===
 
Several of the caveats can be worked around by installing a package that parses a traditional crontab to configure the timers. {{AUR|systemd-cron-next}} and {{AUR|systemd-cron}} are two such packages. These can provide the missing {{ic|MAILTO}} feature.
 
If you like crontabs just because they provide a unified view of all scheduled jobs, {{ic|systemctl}} can provide this. See [[#Management]].
 
== See also ==


* {{man|5|systemd.timer}}
* [https://kofler.info/systemd-timer-als-cron-alternative/ Michael Kofler Blog] Anleitung
* [https://fedoraproject.org/wiki/Features/SystemdCalendarTimers Fedora Project wiki page] on ''systemd'' calendar timers
* [https://fedoraproject.org/wiki/Features/SystemdCalendarTimers Fedora Project wiki page] on ''systemd'' calendar timers
* [https://wiki.gentoo.org/wiki/Systemd#Timer_services Gentoo wiki section] on ''systemd'' timer services
* [https://wiki.gentoo.org/wiki/Systemd#Timer_services Gentoo wiki section] on ''systemd'' timer services
* {{App|systemd-cron-next|tool to generate timers/services from crontab and anacrontab files|https://github.com/systemd-cron/systemd-cron-next|{{Aur|systemd-cron-next}}}}
* [https://www.freedesktop.org/software/systemd/man/systemd.timer.html freedesktop.org]Man page timer
* {{App|systemd-cron|provides systemd units to run cron scripts; using ''systemd-crontab-generator'' to convert crontabs|https://github.com/systemd-cron/systemd-cron|{{Aur|systemd-cron}}}}

Version vom 17. März 2017, 02:51 Uhr

„Systemd/Timers“ wurde von Tuxnix (Diskussion) als in Bearbeitung markiert. Um Bearbeitungskonflikte zu vermeiden, kontaktiere Tuxnix (Diskussion) bitte, bevor du den Artikel bearbeitest.


Timer bieten die Möglichkeit Aufgaben zeitlich zu steuern. Sie bestehen aus einer .timer und einer .service Datei. Der Timer Dienst wird von systemd gesteuert und muss mit dem systemctl Befehl aktiviert werden. Eine Alternative hierzu ist cron.

Beispiel

Die .timer Datei

# Datei /usr/lib/systemd/system/foo.timer
[Unit]
Description=foo timer
[Timer]
OnBootSec=1h
OnUnitInactiveSec=1d
[Install]
WantedBy=basic.target

Die .service Datei

# Datei /usr/lib/systemd/system/foo.service
[Unit]
Description=foo
[Service]
Type=oneshot
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
Environment="ABRT_IGNORE_PYTHON=1"
ExecStart=/usr/bin/foo

Aktivierung

systemctl enable --now foo.timer

Management

systemctl list-timers --all

Listet alle Timer auf.

Weblinks