Systemd/timers: Difference between revisions

From NixOS Wiki
imported>Onny
Add example systemd unit configuration
 
imported>Onny
mNo edit summary
Line 6: Line 6:


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
systemd.timers."faecherstadt-consulting-backup" = {
systemd.timers."hello-world" = {
   wantedBy = [ "timers.target" ];
   wantedBy = [ "timers.target" ];
     timerConfig = {
     timerConfig = {
       OnBootSec = "5m";
       OnBootSec = "5m";
       OnUnitActiveSec = "5m";
       OnUnitActiveSec = "5m";
       Unit = "faecherstadt-consulting-backup.service";
       Unit = "hello-world.service";
     };
     };
};
};


systemd.services."faecherstadt-consulting-backup" = {
systemd.services."hello-world" = {
   script = ''
   script = ''
     set -eu
     set -eu

Revision as of 13:21, 26 November 2022

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 time events, monotonic time events, and can be run asynchronously.

Example usage

Following 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= "nobody";
  };
};