Systemd/timers: Difference between revisions

From NixOS Wiki
Unabomberlive (talk | contribs)
Make page translatable
Unabomberlive (talk | contribs)
Marked this version for translation
Line 2: Line 2:


<translate>
<translate>
<!--T:1-->
Timers are systemd unit files whose name ends in .timer that control .service files or events. Timers can be used as an alternative to <code>cron</code>. Timers have built-in support for calendar-based events and monotonic time events, and can be run asynchronously.
Timers are systemd unit files whose name ends in .timer that control .service files or events. Timers can be used as an alternative to <code>cron</code>. Timers have built-in support for calendar-based events and monotonic time events, and can be run asynchronously.
</translate>
</translate>
<translate>
<translate>
== Configuration ==
== Configuration == <!--T:2-->
</translate>
</translate>
<translate>
<translate>
<!--T:3-->
The following example timer runs a systemd unit every 5 minutes which invokes a bash script.
The following example timer runs a systemd unit every 5 minutes which invokes a bash script.
</translate>
</translate>
Line 32: Line 34:
</syntaxHighlight>
</syntaxHighlight>
<translate>
<translate>
<!--T:4-->
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.
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.
</translate>
</translate>
Line 43: Line 46:
</syntaxHighlight>
</syntaxHighlight>
<translate>
<translate>
<!--T:5-->
More examples can be found at the [https://wiki.archlinux.org/title/Systemd/Timers Arch Wiki] and at the <code>systemd.timer</code> manpage.
More examples can be found at the [https://wiki.archlinux.org/title/Systemd/Timers Arch Wiki] and at the <code>systemd.timer</code> manpage.
</translate>
</translate>
<translate>
<translate>
== Usage ==
== Usage == <!--T:6-->
</translate>
</translate>
<translate>
<translate>
<!--T:7-->
List active timers and their current state:
List active timers and their current state:
</translate>
</translate>
Line 55: Line 60:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:8-->
Manually run a service once for testing purposes:
Manually run a service once for testing purposes:
</translate>
</translate>

Revision as of 07:03, 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.

Configuration

The following example timer runs a systemd unit every 5 minutes which invokes a bash script.

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.

Usage

List active timers and their current state:

systemctl list-timers

Manually run a service once for testing purposes:

systemctl start hello-world