S6 mega thread

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:

  1. After=my-a

    As expected, only setting After= doesn’t affect my-a at all. Let’s check enable:

    As expected, it’s not affected.

BTW, you can start them both at the same time and ordering should be preserved:

  1. Wants=my-a

    As expected, my-a started, and moreover started in parallel, because we didn’t specify that it needs to be running to start my-b (no After=).


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)

  1. Wants=my-a but my-a fails to start

    Again, weak dependency so my-b is fine with starting even if my-a fails.

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 :wink:


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.

  1. Wants=my-a and After=my-a


    As you can see, my-b starts only after my-a finished startup. As expected.

  2. Requires=my-a


    No difference from Wants= here, however stop works 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.

  1. Requires=my-a and After=my-a and broken my-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).
image


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
1 Like