[Nix-dev] Systemd User Timers: timerConfig section?

Christoph Hrdinka c.nix-dev at hrdinka.at
Fri Feb 19 13:22:43 CET 2016


Hi Sergiu,

systemd timers are defined via systemd.timers.<name> (systemwide) or
systemd.user.timers.<name> (per user) and not within *.services.*.

systemd.timers.timerConfig will be used as verbatim systemd timer
config as of:

https://www.freedesktop.org/software/systemd/man/systemd.timer.html

Here is an example configuration:

systemd.user.timers.my-task = {
  description = "run my-task every 5 minutes";
  wantedBy = [ "timers.target" ]; # enable it & auto start it
 
  timerConfig = {
    OnCalendar = "*-*-* *:*/5:00";
  };
};

This timer will start my-task.service every 5mins. This means you have
to add a service as well:

systemd.user.services.my-task = {
  description = "My Task";
  script = "echo hello world";
};

Now systemd will trigger my-task every 5mins. This is however not
always what you want. Another way of doing this is to tell systemd to
start the unit, wait for its completion and then start it again after
5mins.

systemd.user.timers.my-task = {
  description = "run my-task every 5 minutes";
  wantedBy = [ "timers.target" ]; # enable it & auto start it
 
  timerConfig = {
    OnBootSec = "5m"; # first run 5min after boot up
    OnUnitInactiveSec = "5m"; # run 5min after my-task has finished
  };
};

This timer will trigger reruns 5mins after the last run ended. A
service of type "simple" (default) will be set to inactive immediately
after start. If you set it to "oneshot" its activation will last as
long as the run does:

systemd.user.services.my-task = {
  description = "My Task";
  script = "echo hello world";
  serviceConfig.Type = "oneshot";
};

You can test this by running `systemctl start my-task`. With "simple"
it will return immedialtely, with "oneshot" it will return when the
script exits. You can also mix the service/timer units of the above
examples to get different behaviours.

Best regards,
Christoph.

On Fri, 19 Feb 2016 12:11:14 +0100
Sergiu Ivanov <sivanov at colimite.fr> wrote:

> Hello,
> 
> I am trying to set up a (custom) systemd timer running a task every
> 5 minutes.  From what I can see, relying on
> system.user.services.myTimer might be the way to go, but I can only
> see the option startAt available, which starts my unit at a given
> date/time and therefore does not really fit my needs.
> 
> Is there a way to define other timer attributes?
> 
> (I looked for timerConfig, which would be similar to serviceConfig or
> unitConfig, but it doesn't seem to exist.)
> 
> What is the recommended way to go for such periodic tasks under NixOS?
> Do you guys all prefer cron?
> 


More information about the nix-dev mailing list