Discussion:
How to start a task after system boot but only on specific days using systemd.timer
(too old to reply)
Joerg Diederich
2018-01-09 19:28:16 UTC
Permalink
Raw Message
Hello,

I have a little (perhaps strange) requirement:
I want to execute a script about five minutes after system boot, but only on specific weekdays (e. g. Sundays) using systemd.timer. I tried a combination of OnBootSec and OnCalendar like

OnBootSec=5m
OnCalendar=Sun

but this doesn't work. My script executes always after boot. I'm running systemd-228-38.1.x86_64 on SuSE LEAP 42.3. Of course, I can modify my script to ask for the current weekday, but I thought, it would be more elegant to configure it all together in the timer unit file.

Any hint is really appreciated.

Regards,
Joerg
Andrei Borzenkov
2018-01-10 04:17:23 UTC
Permalink
Raw Message
Post by Joerg Diederich
Hello,
I want to execute a script about five minutes after system boot, but only on specific weekdays (e. g. Sundays) using systemd.timer. I tried a combination of OnBootSec and OnCalendar like
OnBootSec=5m
OnCalendar=Sun
but this doesn't work.
Well, it works but not they way you want it :) As documented, multiple
directives are additive - timer will trigger when any of them elapses.
May be it could be expressed more obviously.
Post by Joerg Diederich
My script executes always after boot. I'm running systemd-228-38.1.x86_64 on SuSE LEAP 42.3. Of course, I can modify my script to ask for the current weekday, but I thought, it would be more elegant to configure it all together in the timer unit file.
This would likely require new directive or at least new syntax. Timers
define point in time, not period of time.
Jérémy Rosen
2018-01-10 09:02:18 UTC
Permalink
Raw Message
Post by Andrei Borzenkov
Post by Joerg Diederich
Hello,
I want to execute a script about five minutes after system boot, but only on specific weekdays (e. g. Sundays) using systemd.timer. I tried a combination of OnBootSec and OnCalendar like
OnBootSec=5m
OnCalendar=Sun
but this doesn't work.
Well, it works but not they way you want it :) As documented, multiple
directives are additive - timer will trigger when any of them elapses.
May be it could be expressed more obviously.
There is a workaround but it's a bit more complicated than it should be
: use multiple timers.

timer1.timer ; OnBootSec=5m
timer1.service ; Wants=timer2

timer2.timer ; Oncalendar=Sun


Note that you need the intermediate unit, you cant user
Unit=timer2.timer in timer1.timer because a timer is not allowed to
start another time (I don't know why)

You can also have a look at https://github.com/systemd/systemd/pull/7023
where II suggested something similar for upstream

Cheers
Jérémy
Post by Andrei Borzenkov
Post by Joerg Diederich
My script executes always after boot. I'm running systemd-228-38.1.x86_64 on SuSE LEAP 42.3. Of course, I can modify my script to ask for the current weekday, but I thought, it would be more elegant to configure it all together in the timer unit file.
This would likely require new directive or at least new syntax. Timers
define point in time, not period of time.
_______________________________________________
systemd-devel mailing list
https://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
SMILE <http://www.smile.eu/>

20 rue des Jardins
92600 AsniÚres-sur-Seine


*Jérémy ROSEN*
Architecte technique
Responsable de l'expertise Smile-ECS

email ***@smile.fr <mailto:***@smile.fr>
phone +33141402967
url http://www.smile.eu

Twitter <https://twitter.com/GroupeSmile> Facebook
<https://www.facebook.com/smileopensource> LinkedIn
<https://www.linkedin.com/company/smile> Github
<https://github.com/Smile-SA>


Découvrez l’univers Smile, rendez-vous sur smile.eu
<http://smile.eu/?utm_source=signature&utm_medium=email&utm_campaign=signature>

eco Pour la planÚte, n'imprimez ce mail que si c'est nécessaire
Joerg Diederich
2018-01-10 16:19:34 UTC
Permalink
Raw Message
Post by Jérémy Rosen
Post by Andrei Borzenkov
Post by Joerg Diederich
Hello,
I want to execute a script about five minutes after system boot, but only on specific weekdays (e. g. Sundays) using systemd.timer. I tried a combination of OnBootSec and OnCalendar like
OnBootSec=5m
OnCalendar=Sun
but this doesn't work.
Well, it works but not they way you want it :) As documented, multiple
directives are additive - timer will trigger when any of them elapses.
May be it could be expressed more obviously.
There is a workaround but it's a bit more complicated than it should be
: use multiple timers.
timer1.timer ; OnBootSec=5m
timer1.service ; Wants=timer2
timer2.timer ; Oncalendar=Sun
Note that you need the intermediate unit, you cant user
Unit=timer2.timer in timer1.timer because a timer is not allowed to
start another time (I don't know why)
You can also have a look at https://github.com/systemd/systemd/pull/7023
where II suggested something similar for upstream
Cheers
Jérémy
Hi Jérémy,

thank you very much for this solution. At the moment I'm not quite sure, if I will really implement such a setup. I'm afraid that I will not be able to remember what I'm doing here in a year or so ;) I guess, will stick to a script based solution working with a single timer firing a few minutes after reboot and let the script do the test for the weekend. I think this is easier to understand in case of needed maintenance.

Regards,
Joerg
Lennart Poettering
2018-01-10 17:51:41 UTC
Permalink
Raw Message
Post by Joerg Diederich
Hello,
I want to execute a script about five minutes after system boot, but only on specific weekdays (e. g. Sundays) using systemd.timer. I tried a combination of OnBootSec and OnCalendar like
OnBootSec=5m
OnCalendar=Sun
but this doesn't work. My script executes always after boot. I'm
running systemd-228-38.1.x86_64 on SuSE LEAP 42.3. Of course, I can
modify my script to ask for the current weekday, but I thought, it
would be more elegant to configure it all together in the timer unit
file.
As explained by others this is not how multiple OnXyz lines work in
timer units: they triggers are combined in an OR not an AND fashion,
if you so will...

That said, I wouldn't be opposed to adding a new "ConditionCalendar="
or something, similar to the other ConditionXYZ= settings in unit
files. It would take the same calendar specifications as OnCalendar=
does and would skip execution if the current time does not match a
specific calendar specification. With that it would be possible to
define unit files that only start on friday, or only in july, or only
in the year 2018...

If you'd be interested in that file an RFE bug, or even better send a
patch! Most likely the patch would be less than 20 lines of code...

Lennart
--
Lennart Poettering, Red Hat
Loading...