TIL - Updating systemd service dependency chain

When updating the dependency chain of systemd services, for example by adding a WantedBy property to an existing service definition file:

[Install]
WantedBy=multi-user.target

It is not enough to just restart the service, but the following steps need to be done:

# 1 - Stop service
systemctl stop run_on_shutdown.service
# 2 - Disable service
systemctl disable run_on_shutdown.service
# 3 - Restart systemd
systemctl daemon-reload
# 4 - Enable service
systemctl enable run_on_shutdown.service

If the dependency chain was correctly updated, there should be a multi-user.target.wants directory with a simlink to the updated service, run_on_shutdown:

ls -la /etc/systemd/system/multi-user.target.wants/run_on_shutdown.service 

Similarly, the RequieredBy and UpheldBy properties create the .requires/, and .upholds/ directories.

Other useful commands:

# Get logs since last boot
journalctl -u run_on_shutdown.service -b 0
# Get logs of previous boot
journalctl -u run_on_shutdown.service -b -1
# Check enabled services
systemctl list-unit-files
# Check service dependencies
systemctl list-dependencies --reverse run_on_shutdown.service
# Restart service
systemctl restart run_on_shutdown.service
# Verify systemd service file
systemd-analyze verify /etc/systemd/system/my-service.service