You are not logged in.

#1 2021-12-09 18:16:05

gauthma
Member
Registered: 2010-02-16
Posts: 222
Website

[Solved] systemd-timers: OnActiveSec vs OnUnitActiveSec

Suppose you have a systemd unit foo, defined like this:

    ; file foo.service
    [Unit]
    Description=foo service
    
    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c "echo $(date) >> fff"
    
    [Install]
    WantedBy=timers.target

So it basically appends the date to a file named /home/user/fff. I placed this file in ~/.config/systemd/user, started the service (systemctl --user start foo.service), and so far so good. Now suppose you create a timer for this service, like so:

    ; file foo.timer
    [Unit]
    Description=Timer for foo
    
    [Timer]
    OnActiveSec=3s
    AccuracySec=1us 

(The last line, AccuracySec=1us, is just there because the timeouts here are under a minute, but otherwise can be ignored. Also, at this point both these services are disabled and stopped.)

Now, OnActiveSec refers to the time elapsed since *the timer service* was last run. So if I start foo.timer, foo.service runs... but only once!! Even though the time elapsed since the timer last ran is now way bigger than 3 seconds. I would expect the timer service to run again after the timeout, and, given that it is a timer service, to cause foo.service to also run again, after said timeout passes. However, this is not what happens. Any clue as to why?

But if I add a line with OnUnitActiveSec to the [Timer] section, things work as expected:

    [Timer]
    OnActiveSec=3s
    OnUnitActiveSec=3s
    AccuracySec=1us 

Why? The docs state that OnUnitActiveSec refers to the time since the service referred to by the timer (in our scenario, foo.service) has run. My question is: why is this needed? Shouldn't OnActiveSec suffice? After all, if the triggered is triggered after a timeout, I would expect for the service corresponding to that trigger would also be run -- although this is clearly not happening. Can anyone help clear the issue to me? Thank you in advance.

Last edited by gauthma (2021-12-09 18:39:26)

Offline

#2 2021-12-09 18:27:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,500
Website

Re: [Solved] systemd-timers: OnActiveSec vs OnUnitActiveSec

man systemd.timer wrote:

OnActiveSec=    Defines a timer relative to the moment the timer unit itself is activated.

If you activate the timer unit (ie., `systemctl --user start foo.timer`) once, then it will trigger once when the OnActiveSec duration has passed (3 seconds after you entered the systemctl command).  Why would you expect it to repeat on it's own?  If you want it to repeat every three seconds, you can use an OnCalendar trigger, or OnUnitActiveSec as that's precisely what those triggers are for which is also stated in the same man page:

man systemd.timer wrote:

... by combining OnBootSec= and OnUnitActiveSec=, it is possible to define a timer that elapses in regular intervals and activates a specific service each time.

---

gauthma wrote:

The docs state that OnUnitActiveSec refers to the time since the service referred to by the timer (in our scenario, foo.service) has run. My question is: why is this needed? Shouldn't OnActiveSec suffice?

OnUnitActiveSec does indeed refer to the time since the service referred to by the timer has run.  But if you only use OnUnitActiveSec and no other trigger then issue the command to start or enable foo.timer, foo.service will never run.  Why would it, no trigger would ever be activated in the first place: something needs to trigger the first run of foo.service in order to for you to ever have 3 seconds pass since it was last run.

So in other words, OnUnitActiveSec can be used to define the interval between repetitions, but another trigger (like OnActiveSec or OnBootSec) would be needed to trigger the first run of foo.service to get the ball rolling.

Last edited by Trilby (2021-12-09 18:36:50)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2021-12-09 18:38:41

gauthma
Member
Registered: 2010-02-16
Posts: 222
Website

Re: [Solved] systemd-timers: OnActiveSec vs OnUnitActiveSec

Trilby wrote:

But if you only use OnUnitActiveSec and no other trigger then issue the command to start or enable foo.timer, foo.service will never run.  Why would it, no trigger would ever be activated.

Yes, this I know. I wanted to do the opposite, ie, use only OnActiveSec. But you answered my question already. I had read those docs with a "cron job mindset", and so I was thinking of timers as necessarily periodical (which is of course, not necessarily true: you can run a timer just once, for example...)

Anyway, thanks for the answer, marking as solved :-)

Offline

Board footer

Powered by FluxBB