Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 589
Nayana Madhu Avatar asked Apr 04 '18 05:04

Nayana Madhu


People also ask

What is ExecStart in Linux?

ExecStart. The commands and arguments executed when the service starts. ExecStartPre, ExecStartPost. Additional commands that are executed before or after the command in ExecStart . ExecReload.

What is multi user target?

multi-user. target: This target is often used as the default target a system starts in. It starts everything that is needed for full system functionality and is commonly used on servers. graphical.

What is a systemd target?

systemd targets are different states that your system can boot into, comparable to System V runlevels. Unlike SysV runlevels, target units are named rather than numbered. For example, the graphical. target is comparable to SysV runlevel 5, multiuser with network and a graphical environment.

What is a .service file?

The database server requires a Services file, which contains information about the known services on your network. The Services file is typically located in %windir%\System32\drivers\etc\services.


2 Answers

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

like image 65
Tabinol Avatar answered Sep 27 '22 20:09

Tabinol


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.

like image 39
iamauser Avatar answered Sep 27 '22 20:09

iamauser