Abstracted example:
I have two recurring tasks A
and B
that I want to setup as systemd
services. As the tasks are quite similar to each other, I am using a template [email protected]
; A and B will be running as instances of that template: [email protected]
and [email protected]
, respectively.
# [email protected]
[Unit]
Description= myservice %I
[Service]
Type=oneshot
IOSchedulingClass=idle
ExecStart=bash -c "echo Starting %I; sleep 10; echo Finished %I"
Restart=on-failure
The recurring requirement of the two tasks should be handled via two timers, myservice-A.timer
and myservice-B.timer
. The timers will make sure that the two tasks are repeated according to configured time intervals, e.g. A
on a weekly basis and B
on a daily basis. The timers guarantee this even for the case that the PC was shut off at that time, in which case they will simply start the service as soon as possible when the pre-configured restart time has surpassed.
This last case however, can conflict with the requirement that A
has to finish before B
(if both are started simultaneously). As I am using template instances, I cannot use the Before=
and After=
options on the service itself. I can add them to the timers, but this does not seem to solve the problem (probably because now the timers just get an ordering dependency, but not the services they are starting).
# myservice-A.timer
[Unit]
# The Before= requirement below does not help
Before=myservice-B.timer
Description=weekly execution of myservice
[Timer]
OnCalendar=Monday *-*-* 09:20:00
Persistent=true
[email protected]
[Install]
WantedBy=timers.target
# myservice-B.timer
[Unit]
# the After= requirement below does not help
After=myservice-A.timer
Description=daily execution of myservice
[Timer]
OnCalendar=09:30:00
Persistent=true
[email protected]
[Install]
WantedBy=timers.target
Note that B
does not require execution of A
, i.e. I am not looking for a dependency of the type Requires=
. I am only talking about the case that when both A
and B
are started via their timers, then A
has to have finished before B
is started.