9

I've got two systemd timers and their associated services:

  • a.timer triggering a.service
  • b.timer triggering b.service

Both services perform housekeeping tasks using the system package manager so they must not be run in parallel (in fact, either service fails if it is started while the other is running, as both try to create a lock file).

My aim is to ensure that both services do get run so b.service shall be delayed if a.service is running and vice versa.

If I understand systemd.unit(5) correctly I cannot use Conflicts= as this would actively stop one service when the other one is being started. I cannot use After= or Before= either (with Type=simple) as this would delay the second service only until the first one is initialized (i.e. still running).

One solution, to my understanding, would be the combination of Type=oneshot and After= as oneshot makes systemd consider the unit active (and immediately dead) after the main process has terminated. On the other hand, using Type=oneshot is explicitly discouraged for possibly long-running tasks.

One could also put both tasks in one .service unit using

ExecStart=/usr/bin/sh -c 'first-command; second-command'

but I'd consider this very ugly as it would bind the execution interval of both services together.

So what is the most systemd'ish way of having multiple services that depend on the same exclusive resource?

1
  • 3
    There's a relevant thread on the mailing list, started on the same day as this post. For the benefit of others - the suggestions were to handle the blocking lock externally to systemd, using flock or a blocking lock in the target application itself.
    – Chris Hunt
    Oct 10, 2020 at 14:30

0

You must log in to answer this question.

Browse other questions tagged .