Systemd/timers/en: Difference between revisions

FuzzyBot (talk | contribs)
Updating to match new version of source page
 
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
Line 25: Line 25:
};
};
</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.&lt;name&gt;.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.&lt;name&gt;.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====
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.
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">