Systemd/timers/ru: Difference between revisions
Created page with "Следующий пример таймера запускает каждые 5 минут юнит systemd, который вызывает сценарий bash." |
Updating to match new version of source page Tags: Mobile edit Mobile web edit |
||
(6 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
{{Systemd/breadcrumb}} | {{Systemd/breadcrumb}} | ||
Таймеры - это файлы модулей systemd, чье имя заканчивается на .timer, которые управляют .service файлами или событиями. Таймеры могут быть использованы в качестве альтернативы <code>cron</code>. | |||
Таймеры имеют встроенную поддержку событий, основанных на календаре, и монотонных временных событий, а также могут запускаться асинхронно. | |||
<span id="Configuration"></span> | <span id="Configuration"></span> | ||
== Настройка == | == Настройка == | ||
Line 28: | Line 27: | ||
}; | }; | ||
</syntaxHighlight> | </syntaxHighlight> | ||
< | |||
Alternatively here, avoid quotes when calling for the binary and its command options: | |||
<syntaxHighlight lang="nix"> | |||
${pkgs.foo}/bin/foo command-options | |||
</syntaxHighlight> | |||
This will yield the same result as running | |||
<syntaxhighlight lang="bash"> | |||
foo command-options | |||
</syntaxhighlight> | |||
in your terminal. | |||
====Using the <code>systemd.services.<name>.startAt</code> shorthand==== | |||
If you only want a service to execute at an interval and don't plan to configure the timer much more, you can use the <code>systemd.services.<name>.startAt</code> option. This will have the underlying systemd module in nixpkgs create the timer for you, and set its <code>OnCalendar</code> field. Note that the semantics for <code>OnCalendar</code> are different to <code>OnUnitActiveSec</code>. | |||
This example shows the previous <code>hello-world</code> service configured with <code>startAt</code>, running every 5 minutes. | |||
<syntaxHighlight lang="nix"> | |||
systemd.services."hello-world" = { | |||
script = '' | |||
set -eu | |||
${pkgs.coreutils}/bin/echo "Hello World" | |||
''; | |||
serviceConfig = { | |||
Type = "oneshot"; | |||
User = "root"; | |||
}; | |||
startAt = "*:0/5"; | |||
}; | |||
</syntaxHighlight> | |||
====Running timer on a schedule==== | |||
Следующий пример запускается один раз в день (в 12:00). При активации он запускает службу немедленно, если пропущено время последнего запуска (опция Persistent=true), например, из-за отключения питания системы. | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
... | ... | ||
Line 44: | Line 77: | ||
<span id="Usage"></span> | <span id="Usage"></span> | ||
== Использование == | == Использование == | ||
Список активных таймеров и их текущее состояние: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl list-timers | systemctl list-timers | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Запустите службу вручную один раз в целях тестирования: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
systemctl start hello-world | systemctl start hello-world | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:systemd]] | [[Category:systemd]] |