Systemd/timers: Difference between revisions
Marked this version for translation |
added alternative example of the call for the binary and command, the quotes confused me when replicating this for rclone (maybe more me but didn't seem like it hurt to add) |
||
Line 33: | Line 33: | ||
}; | }; | ||
</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. | |||
====Running timer on a schedule==== | |||
<translate> | <translate> | ||
<!--T:4--> | <!--T:4--> |
Revision as of 17:39, 15 September 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";
};
};
Alternatively here, avoid quotes when calling for the binary and its command options:
${pkgs.foo}/bin/foo command-options
This will yield the same result as running
foo command-options
in your terminal.
Running timer on a schedule
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