Systemd/timers/ru: Difference between revisions

From NixOS Wiki
Unabomberlive (talk | contribs)
Created page with "== Настройка =="
Unabomberlive (talk | contribs)
Created page with "Следующий пример таймера запускает каждые 5 минут юнит systemd, который вызывает сценарий bash."
Line 6: Line 6:
<span id="Configuration"></span>
<span id="Configuration"></span>
== Настройка ==
== Настройка ==
<div lang="en" dir="ltr" class="mw-content-ltr">
Следующий пример таймера запускает каждые 5 минут юнит systemd, который вызывает сценарий bash.
The following example timer runs a systemd unit every 5 minutes which invokes a bash script.
</div>
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
systemd.timers."hello-world" = {
systemd.timers."hello-world" = {

Revision as of 07:06, 6 August 2024

Timers are systemd unit files whose name ends in .timer that control .service files or events. Timers can be used as an alternative to cron. Timers have built-in support for calendar-based events and monotonic time events, and can be run asynchronously.

Настройка

Следующий пример таймера запускает каждые 5 минут юнит systemd, который вызывает сценарий bash.

systemd.timers."hello-world" = {
  wantedBy = [ "timers.target" ];
    timerConfig = {
      OnBootSec = "5m";
      OnUnitActiveSec = "5m";
      Unit = "hello-world.service";
    };
};

systemd.services."hello-world" = {
  script = ''
    set -eu
    ${pkgs.coreutils}/bin/echo "Hello World"
  '';
  serviceConfig = {
    Type = "oneshot";
    User = "root";
  };
};

The following example starts once a day (at 12:00am). When activated, it triggers the service immediately if it missed the last start time (option Persistent=true), for example due to the system being powered off.

...
  timerConfig = {
      OnCalendar = "daily";
      Persistent = true; 
  };
};

More examples can be found at the Arch Wiki and at the systemd.timer manpage.

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

List active timers and their current state:

systemctl list-timers

Manually run a service once for testing purposes:

systemctl start hello-world