Best practice for Wants= vs WantedBy= in Systemd Unit Files
Functionally
Wants
is in the Unit
section and WantedBy
is in the Install
.
The init process systemd
does not process/use the Install
section at all. Instead, a symlink must be created in multi-user.target.wants
. Usually, that's done by the utility systemctl
which does read the Install
section.
In summary, WantedBy
is affected by systemctl enable
/systemctl disable
.
Logically
Consider which of the services should "know" or be "aware" of the other. For example, a common use of WantedBy
:
[Install]
WantedBy=multi-user.target
Alternatively, that could be in multi-user.target:
[Unit]
Wants=nginx.service
But that second way doesn't make sense. Logically, nginx.service knows about the system-defined multi-user.target, not the other way around.
So in your example, if alpha's author is aware of beta, then alpha Wants
beta. If beta's author is aware of alpha then beta is WantedBy
alpha.
To help you decide, you may consider which service can be installed (say, from a package manager) without the other being present.
Config directories
As another tool in your box, know that systemd files can also be extended with config directories: /etc/systemd/system/myservice.service.d/extension.conf
This allows you to add dependencies where neither service is originally authored to know about the other. I often use this with mounts, where (for example) neither nginx nor the mount need explicit knowledge of the other, but I as the system adminstrator understand the dependency. So I create nginx.service.d/mymount.conf
with Wants=mnt-my.mount
.