Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Systemd: Using both After and Requires

I have a service foo.service which depends on service bar.service. I need to make sure that bar.service is started before foo.service and that bar.service launched successfully.

From this source it says that Requires:

This directive lists any units upon which this unit essentially depends. If the current unit is activated, the units listed here must successfully activate as well, else this unit will fail. These units are started in parallel with the current unit by default.

and that After:

The units listed in this directive will be started before starting the current unit. This does not imply a dependency relationship and one must be established through the above directives if this is required.

Is it correct to have both the Requires and After sections in the same unit file? Requires says that the service will be launched in parallel, but After says it will be launched before. If bar.service fails to start during the After condition, will it attempt to launch it again during the Requires section? If so I need to find another way to launch foo.service

foo.service

[Unit]
After=bar.service
Requires=bar.service
like image 974
flakes Avatar asked Nov 14 '16 16:11

flakes


People also ask

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 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 does after mean in systemd?

After= is putting a start order between services. If both of the services are scheduled to start, then After= makes sure the start order is set. You can look at systemd's own service file as an example. /lib/systemd/system/basic.

What is WantedBy?

The line WantedBy=multi-user. target in a service is essentially the same as specifying "this service should start in runlevels 3, 4 and 5" in SysVinit systems: it tells systemd that this service should be started as part of normal system start-up, whether or not a local GUI is active.


1 Answers

While Umut's answer is correct, there is an interaction between Requires= and After= that is rarely spelled out. From systemd.unit#Requires= (emphasis is mine):

If this unit gets activated, the units listed will be activated as well. If one of the other units fails to activate, and an ordering dependency After= on the failing unit is set, this unit will not be started.

Essentially, this means that without After=, both services will be started if foo.service is started (because of Requires=), but systemd will not stop foo.service if bar.service does not start successfully.

However, with the added After=, it will wait for bar.service to start successfully (with some exceptions; see the note in the Requires= docs) before it starts foo.service and won't start it if bar.service doesn't start.

Somewhat related is the difference between Requires= and BindsTo= (see the docs around the link above).

like image 108
Gerhard Avatar answered Sep 23 '22 05:09

Gerhard