Systemd/timers: Difference between revisions

From NixOS Wiki
imported>Onny
Further usage info
imported>SuperSandro2000
No edit summary
Line 22: Line 22:
   serviceConfig = {
   serviceConfig = {
     Type = "oneshot";
     Type = "oneshot";
     User= "nobody";
     User = "root";
   };
   };
};
};

Revision as of 20:36, 19 May 2023

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.

Configuration

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";
  };
};

Usage

List active timers and their current state

systemctl list-timers

Manually run service once for testing purpose

systemctl start hello-world