Systemd: How to make two services mutually exclusive but run both?

2

1

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?

lynix

Posted 2019-10-14T07:43:14.393

Reputation: 61

No answers