3

I have several jobs, which I want systemd to start periodically but with an offset to each other.

Since the jobs are quite similar, I want to cover all of them with a single systemd template unit.

However, since the jobs might cause each some load, I want to disentangle the start times and use different systemd timers for each actual service.

Is there a way to hand over additional arguments (as a specific start time) during creating a service based on the template?

systemctl start MyGeneralService@Specific1.{AdditionalArgument??}.service

or is there a way to read a variable later to be used from within the unit template

[Service]
MyVar = {?XYZ?} /etc/MyService.d/%i.conf {?XYZ?}

[Timer]
OnCalendar=*:0/{?MyVar?}

or is there any way to define dependencies in a template for derived services? I.e., Before=/After= of derived services in a dynamic(??) way

Specific2
[Service]
After=MyGeneralService@{?%i-1?}.service

1 Answer 1

6

May I necropost here? :)

You need a timer, that one can create a bunch of for one service by specifying Unit. E.g:

# systemctl edit --full --force [email protected]
...
[Timer]
Unit=MyGeneralService@%i.service
RandomizedDelaySec=30
OnActiveSec=5 #start on timer activation after 5 sec once 
OnUnitActiveSec=1w #start on timer activation each 1 week pass
...

If you want more generic (an dirty) way, you can pass 2nd arg by utilizing a %j token:

  1. Create a generic service/timer.
  2. Make hardlinks to that service/timer with desired argument as last prefix word, delimited by -

Example:

# systemctl edit --full --force [email protected]
...
[Timer]
Unit=MyGeneralService@%i.service
OnUnitActiveSec=%J
...

# ln /etc/systemd/system/[email protected] /etc/systemd/system/[email protected]
# ln /etc/systemd/system/[email protected] /etc/systemd/system/[email protected]
# systemctl daemon-reload
# systemctl show [email protected]
...
[email protected]
TimersMonotonic={ OnUnitActiveUSec=1w ; next_elapse=0 }
...
# systemctl show [email protected]
...
[email protected]
TimersMonotonic={ OnUnitActiveUSec=1d ; next_elapse=0 }
...

PS: Quick timers hands-on by Arch

1
  • uh, hardlinks are quite a nice hack - I like that ;)
    – THX
    Mar 5, 2019 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .