1

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

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 MyGeneralService-1w@.timer
...
[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 MyGeneralService@.timer
...
[Timer]
Unit=MyGeneralService@%i.service
OnUnitActiveSec=%J
...

# ln /etc/systemd/system/MyGeneralService@.timer /etc/systemd/system/MyGeneralService-1w@.timer
# ln /etc/systemd/system/MyGeneralService@.timer /etc/systemd/system/MyGeneralService-1d@.timer
# systemctl daemon-reload
# systemctl show MyGeneralService-1w@foo.timer
...
Triggers=MyGeneralService@foo.service
TimersMonotonic={ OnUnitActiveUSec=1w ; next_elapse=0 }
...
# systemctl show MyGeneralService-1d@bar.timer
...
Triggers=MyGeneralService@bar.service
TimersMonotonic={ OnUnitActiveUSec=1d ; next_elapse=0 }
...

PS: Quick timers hands-on by Arch

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.