[systemd-devel] run only a single service at a time

Vito Caputo vcaputo at pengaru.com
Fri Oct 29 14:38:40 UTC 2021


On Fri, Oct 29, 2021 at 12:52:06PM +0200, Leon Fauster wrote:
> Am 29.10.21 um 07:47 schrieb Andrei Borzenkov:
> > On 28.10.2021 16:04, Olaf Hering wrote:
> > > There is A.timer and its A.service, and B.timer and B.service.
> > > Both A and B do not know about each other per default.
> > > Both timers fire in their own cadence.
> > > Both services have their unpredictable time until they finish.
> > > So it may happen that A.service is started while B.service is still active.
> > > 
> > > But, unfortunately both services may touch the same files while they run.
> > > 
> > > Does systemd have a way to serialize execution of such units, so that starting of A.service is delayed until B.service is done, and via versa?
> > > 
> > 
> > Not that I am aware of. Similar questions (or requests) came up in the past.
> 
> 
> 
> Maybe ExecCondition= can be used with a customscript (or ExecStartPre=)?
> 

If one still wants both executions to occur in the collision case,
just serialized, it seems like a simple `flock` wrapper scenario.

i.e. define a lock file path somewhere for serializing access to the
shared files, and pass that lock file to both service executions:
 `flock /path/to/lockfile -c service-command-to-serialize-on-lockfile`

This way when they collide they just wait for eachother unbeknownst to
systemd, and will run serially in quick succession.

One could argue that if the underlying service is sharing these files,
it should probably be serializing access itself to prevent stepping on
its own toes.  That would likely be done the same way using an
advisory lock on a shared file, maybe in a simple startup script using
flock as well.

Regards,
Vito Caputo


More information about the systemd-devel mailing list