Specify systemd dependency on any one of multiple units?

Daniel Kauffman asked:

Is it possible to specify a systemd dependency on any one of multiple units?

Currently, I have a unit Z that depends on at least one of unit A or unit B.

Configuring unit A to be WantedBy unit Z and unit B to be WantedBy unit Z mostly works.

However, if only one of units A or B is able to start, the boot process waits for the other unit to timeout before starting unit Z. I want to eliminate this timeout.

Units A and B require a timeout in order to function properly, however, once once of them has started, there is no need to wait for the other unit to timeout before starting unit Z.

Is there a way to specify that unit Z depends on either unit A or unit B, but does not need to wait for both unit A and unit B before starting unit Z?

My answer:


This is a job for a systemd target unit.

Rather than give a contrived example, I’ll give a real world example which is already present on your system.

Consider NTP. Most computers sync via NTP, but there are three (and maybe more) NTP clients that you might choose from: systemd-timesyncd, chronyd or (the classic) ntpd.

Each of the service units for these NTP clients is part of a target called time-sync.target, and weakly requires it as such. Note that time-sync.target itself is empty and doesn’t actually do anything on its own.

Before=time-sync.target
Wants=time-sync.target

Thus, when you start any of the NTP clients, time-sync.target is started up after the NTP client starts.

So, if you run a service which requires that the system time has been synchronized, and would fail otherwise, you can require it to start only after time-sync.target has started.

After=time-sync.target
Requires=time-sync.target

You should easily be able to adapt this to your own service.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.