最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

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
Share edited Feb 10 at 14:22 ni_hao asked Feb 10 at 13:45 ni_haoni_hao 4282 gold badges8 silver badges17 bronze badges 5
  • as a side note, if you are bound to Python 2.7, at least check if you can run it with pypy 2.7, instead of Python, as pypy is still maintained and developed. – jsbueno Commented Feb 10 at 14:07
  • Thanks but I can not use/run pypy – ni_hao Commented Feb 10 at 14:20
  • 1 24h is not multiple of 2.5 h. So it's not clear when you say "00.00, 02:30, 05:00, 07.30, 10:00, etc" what does it mean. You will eventually arrive to 22:30 and then what? After 2.5 h it will be 1:00 so the times you mentioned are just for the very first day? – Cincinnatus Commented Feb 10 at 14:28
  • thanks. So I have to change [Timer] with dedicated hours... ``` [Timer] OnCalendar=00:00 OnCalendar=02:30 OnCalendar=05:00 OnCalendar=07:30 OnCalendar=10:00 OnCalendar=12:30 OnCalendar=15:00 OnCalendar=17:30 OnCalendar=20:00 OnCalendar=22:30 Persistent=true ``` – ni_hao Commented Feb 10 at 14:47
  • 1 Do you want this just to run every 150 minutes or do you want a fixed time 5 day rotation? – JonSG Commented Feb 10 at 14:53
Add a comment  | 

1 Answer 1

Reset to default 2

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=.)

发布评论

评论列表(0)

  1. 暂无评论