Difference between revisions of "Systemd (Русский)/Timers (Русский)"

From ArchWiki
Jump to navigation Jump to search
Line 146: Line 146:
== Смотрите также ==
== Смотрите также ==
* [http://www.freedesktop.org/software/systemd/man/systemd.timer.html systemd.timer man page] on freedesktop.org
* [http://www.freedesktop.org/software/systemd/man/systemd.timer.html systemd.timer man page] на freedesktop.org (Англ.)
* [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/ru#.D0.A1.D0.B5.D1.80.D0.B2.D0.B8.D1.81.D1.8B_.D1.82.D0.B0.D0.B9.D0.BC.D0.B5.D1.80.D0.BE.D0.B2 Раздел Gentoo wiki] Сервисы таймеров ''systemd''
* {{App|systemd-cron-next|tool to generate timers/services from crontab and anacrontab files|https://github.com/kstep/systemd-cron-next|{{Aur|systemd-cron-next}}}}
* {{App|systemd-cron-next|утилита для создания таймеров/служб из файлов crontab и anacrontab |https://github.com/kstep/systemd-cron-next|{{Aur|systemd-cron-next}}}}
* {{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}}}}
* {{App|systemd-cron|предоставляет юнитам systemd запускать скрипты cron; используя ''systemd-crontab-generator'' для конвертации crontab'ов|https://github.com/systemd-cron/systemd-cron|{{Aur|systemd-cron}}}}

Revision as of 11:46, 27 March 2016


Состояние перевода: На этой странице представлен перевод статьи Systemd/Timers. Дата последней синхронизации: 24 марта 2016. Вы можете помочь синхронизировать перевод, если в английской версии произошли изменения.

Таймеры это файлы служб systemd имя которых оканчивается на .timer они контролируют файлы .service или события. Таймеры могут использоваться как альтернатива cron (читайте #В качестве замены cron). Таймеры имеют встроенную поддержку временных календарных событий, монотонных временных событий, и могут быть запущены в асинхронном режиме.

Юниты таймера

Таймеры systemd это файлы юнитов с суфиксом .timer. Таймеры, как и другие файлы файлы настроек юнитов и загружаются по одному и тому же пути, но включают в себя секцию [Timer]. Секция [Timer] определяет, когда и как таймер активизируется. Таймеры определяются в качестве одного из двух типов:

  • Монотонный таймер активируется после определенного промежутка времени по отношению к той или иной отправной точки. Есть несколько различных монотонных таймеров, но все они имеют вид: OnTypeSec=. OnBootSec и OnActiveSec являются общими монотонными таймерами.
  • Таймер реального времени (также известный как таймер настенный часы) активируется на события календаря (как cronjobs). Для их определения используется опция OnCalendar=.

Для полного объяснения опций таймера смотрите systemd.timer(5) странице справочного руководства. Синтаксис аргументов для событий календаря и промежутка времени определяется на systemd.time(7) странице справочного руководства.

Служба юнита

For each .timer file, a matching .service file exists (e.g. foo.timer and foo.service). The .timer file activates and controls the .service file. The .service does not require an [Install] section as it is the timer units that are enabled. If necessary, it is possible to control a differently-named unit using the Unit= option in the timer's [Timer] section.


To use a timer unit enable and start it like any other unit (remember to add the .timer suffix). To view all started timers, run:

$ 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
  • To list all timers (including inactive), use 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 stamp-* file in /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.


No changes to service unit files are needed to schedule them with a timer. The following example schedules foo.service to be run with a corresponding timer called foo.timer.

Монотонный таймер

A timer which will start 15 minutes after boot and again every week while the system is running.

Description=Run foo weekly and on boot



Таймер в реальном времени

A timer which starts once a week (at 12:00am on Monday). It starts once immediately if it missed the last start time (option Persistent=true), for example due to the system being powered off:

Description=Run foo weekly

Совет: Special event expressions like daily and 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 RandomizedDelaySec option avoids this problem by randomly staggering the start time of each timer. See systemd.timer (5).

В качестве замены cron

Although cron is arguably the most well-known job scheduler, systemd timers can be an alternative.


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 the systemd.exec(5) man page).
  • 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.


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 systemctl commands. Compare that to adding a single line to a crontab.
  • Emails: there is no built-in equivalent to cron's MAILTO for sending emails on job failure. See the next section for an example of setting up an equivalent using OnFailure=.


You can set up systemd to send an e-mail when a unit fails - much like Cron does with MAILTO. 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 sendmail:


/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")

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:

Description=status email for %I to user1

ExecStart=/usr/local/bin/systemd-email user1@mailhost %i

First notice that the unit to send email about is an instance parameter, so this one service can be used to send email for many other units. However the recipient is hard-coded (since unit templates can only take a single parameter) so you will need to create multiple services if you want to send emails to different sets of recipients. At this point you should test the service to verify that you can receive the emails:

# systemctl start status-email-user1@dbus.service

Then simply edit the service you want emails for and add OnFailure=status-email-user1@%n.service to the [Unit] section. %n passes the unit's name to the template.

Примечание: If you set up SSMTP security according to SSMTP#Security the user nobody will not have access to /etc/ssmtp/ssmtp.conf, and the systemctl start status-email-user1@dbus.service command will fail. One solution is to use root as the User in the status-email-user1@.service module.

Использование crontab

Several of the caveats can be worked around by installing a package that parses a traditional crontab to configure the timers. systemd-cron-nextAUR and systemd-cronAUR are two such packages. These can provide the missing MAILTO feature.

If you like crontabs just because they provide a unified view of all scheduled jobs, systemctl can provide this. See #Management.

Смотрите также

https://github.com/kstep/systemd-cron-next || systemd-cron-nextAUR
  • systemd-cron — предоставляет юнитам systemd запускать скрипты cron; используя systemd-crontab-generator для конвертации crontab'ов
https://github.com/systemd-cron/systemd-cron || systemd-cronAUR