pythontimersystemd

python systemd timer every 2.5 hrs past the hour, but it runs every 2 hrs


I am forced/bound to use python 2.7. I have created a systemd timer (and system.service of course) where the timer should run (24/7) every 2.5 hours past the hour. So starting at 00.00, 02:30, 05:00, 07.30, 10:00, etc). With the source below the timer runs, but (according to systemctl list-timers) it runs unfortunately every 2 hours.

What is wrong with this systemd timer file:

[Unit]
Description=Run every 2:30 Hours

[Timer]
OnCalendar=00/2:30
Persistent=true

[Install]
WantedBy=timers.target

Solution

  • The question has nothing to do with Python, really, but regardless – the problem is that the / operator in systemd's calendar notation only applies to that specific field, not to the timestamp as a whole. Your 00/2:30 is not "every 2:30" – it is interpreted as 00/2 hours and 30 minutes separately from each other (i.e. "every 2 hours starting from 00:xx" combined with "xx:30 minutes").

    There is no way to repeat the 'hours' field by a non-integral amount of hours. If you want the iterations to restart every midnight (because, as was mentioned in the comments, a day is not an exact multiple of 2.5hr), you would have to write out each iteration explicitly:

    OnCalendar=00,05,10,15,20:00
    OnCalendar=02,07,12,17,22:30
    

    (Use systemd-analyze calendar --iterations=5 ... to test OnCalendar values.)

    If you do not want the iterations to restart every midnight but to continue offset (i.e. 25:00 becoming the next day's 01:00, 03:30, 06:00...), then I suspect there is no easy way to express that using OnCalendar. It is, however, possible to achieve using the non-calendar modes of .timer units:

    OnActiveSec=0s
    OnUnitActiveSec=2h30min
    

    This would just keep re-triggering the service unit every 2h30min after the previous trigger. (Approximately; see also AccuracySec= and RandomizedDelaySec=.)