If you will
So, first of all, After= is not a dependency per se, it’s just an ordering requirement.
Wants= and Requires= are dependencies.
Let’s take the following “base” services:
# cat my-a.service
[Unit]
Description=My A
[Service]
RemainAfterExit=yes
Type=oneshot
ExecStart=echo "Starting My A"
ExecStart=sleep 3
ExecStart=echo "Started My A"
ExecStop=echo "Stopping My A"
ExecStop=sleep 3
ExecStop=echo "Stopped My A"
[Install]
WantedBy=multi-user.target
# cat my-b.service
[Unit]
Description=My B
After=my-a.service
[Service]
RemainAfterExit=yes
Type=oneshot
ExecStart=echo "Starting My B"
ExecStart=sleep 3
ExecStart=echo "Started My B"
ExecStop=echo "Stopping My B "
ExecStop=sleep 3
ExecStop=echo "Stopped My B"
[Install]
WantedBy=multi-user.target
By default, both services are disabled, but you can start them on demand:
Now let’s see what happens if I add to my-b.service:
After=my-a
As expected, only settingAfter=doesn’t affect my-a at all. Let’s checkenable:
As expected, it’s not affected.
BTW, you can start them both at the same time and ordering should be preserved:
Wants=my-a
As expected,my-astarted, and moreover started in parallel, because we didn’t specify that it needs to be running to startmy-b(noAfter=).
A bit questionable if stopping
my-b should stop my-a since it brought it up as a requirement, but I can kinda sorta understand that.
my-b can work without my-a (weak dependency)
Wants=my-abutmy-afails to start
Again, weak dependency somy-bis fine with starting even ifmy-afails.
One reason this is the case is that you use Wants= or rather WantedBy= to specify targets. If you used Requires= or RequiredBy= your whole startup process could fail if one component failed. And - in my opinion - it’s better to start in a degraded state and debug than bail out because your VPN couldn’t connect or something ![]()
Oh, and one more thing, this is the same in both cases with Wants=, but:
IIRC that’s because
enable/disable only looks at the [Install] unit section.If a unit is pulled via the
.wants/ drop-in, sysd can read the [Unit] section and pull the remaining dependencies on its own.
-
Wants=my-aandAfter=my-a
As you can see, my-b starts only after my-a finished startup. As expected. -
Requires=my-a
No difference fromWants=here, howeverstopworks a bit differently:
Breaking my-a has an interesting effect on my-b:
My guess here is that since they started in parallel
my-b got into the active state before its dependency failed. IMO the service should stop at this point.
Requires=my-aandAfter=my-aand brokenmy-a
This time, as expected, the service didn’t even begin startup because its dependency failed.
Note that in all of these cases, both my-a and my-b were disabled in systemd terms, or rather were not enabled. Except for enable/disable there is also mask and unmask that work differently. Masking creates a symlink from /etc/systemd/system/ to /dev/null to make it impossible to load the unit. I can’t mask my-a because it’s already there, but I can mask my-c and change my-b to point to my-c instead (for science).

Requires=andAfter=is a no-go:

Wants=andAfter=works just as brokenmy-a:
I hope this research answers all your questions regarding Requires/Wants/After.
Oh and one more thing (vm using Ubuntu focal):
marandil@mdl-vm:~$ egrep "^\[Unit\]" -R /usr/lib/systemd/system | wc -l
417
marandil@mdl-vm:~$ egrep "^After=" -R /usr/lib/systemd/system | wc -l
263
marandil@mdl-vm:~$ egrep "^Wants=" -R /usr/lib/systemd/system | wc -l
63
marandil@mdl-vm:~$ egrep "^Requires=" -R /usr/lib/systemd/system | wc -l
83













