Systemd: Using both After and Requires

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).


It is perfectly fine to use both After= and Requires=. They have different purposes. Requires= sets up a start dependency. systemd makes sure that if any body is trying to start foo.service, it should start bar.service too. Should bar.service fails at some point, then foo.service is taken down too.

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.target
[Unit]
...
Requires=sysinit.target
After=sysinit.target
...