Nayana Madhu
Nayana Madhu

Reputation: 1225

Systemd service B to start after another service A only if Service A exists

I have two systemd services A and B . I want B to get executed after A but only if A exists else just execute B .

Upvotes: 10

Views: 13581

Answers (2)

iamauser
iamauser

Reputation: 11469

SerivceB needs to include Wants= directive for ServiceA. Unit section of ServiceB.service file would look something like this:

[Unit]
Description=ServiceB description
Wants=ServiceA.service

From https://www.freedesktop.org/software/systemd/man/systemd.unit.html

Wants= A weaker version of Requires=. Units listed in this option will be started if the configuring unit is. However, if the listed units fail to start or cannot be added to the transaction, this has no impact on the validity of the transaction as a whole. This is the recommended way to hook start-up of one unit to the start-up of another unit.

Note that dependencies of this type may also be configured outside of the unit configuration file by adding symlinks to a .wants/ directory accompanying the unit file. For details, see above.

Upvotes: 5

Tabinol
Tabinol

Reputation: 326

You need to add in your B.service file:

After=A.service

Or you can add to A.service:

Before=B.service

From SystemD documentation:

Before=, After=

These two settings expect a space-separated list of unit names. They configure ordering dependencies between units. If a unit foo.service contains a setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service has finished starting up. Note that this setting is independent of and orthogonal to the requirement dependencies as configured by Requires=, Wants= or BindsTo=.

Documentation source: https://www.freedesktop.org/software/systemd/man/systemd.unit.html

Upvotes: 5

Related Questions