brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: PartOf= makes a systemd service stop and restart along with another one

Systemd
~/tips/tip-systemd-partof-restart-together

sudo systemctl edit myapp-worker.service

Lets a helper service follow its main service when that one stops or restarts.

Apps that come as a main service plus a helper (a worker or a small sidecar script) have an annoying habit: you restart the main one and the helper keeps running with old state. PartOf= ties the helper to its parent.

Put it in the helper’s unit. When systemd stops or restarts the unit you name, it does the same to the helper:

sudo systemctl edit myapp-worker.service

[Unit]
PartOf=myapp.service
After=myapp.service

Now sudo systemctl restart myapp.service restarts myapp-worker.service too, and systemctl stop myapp.service takes both down. On the parent the link shows up the other way round, as ConsistsOf=, which you can check with:

systemctl show -p ConsistsOf myapp.service

That is the one-liner I’d use to see which helpers ride along before restarting something on a server.

The caveat is in the manual’s wording: it is “limited to stopping and restarting”. Starting is not passed on. If myapp.service starts at boot, the worker does not start with it unless something else pulls it in, so give the worker its own WantedBy= in [Install] (or add Wants=myapp-worker.service to the parent). It is also one-way. Restarting the worker leaves the main service alone, which is usually exactly what you want. If you need the stronger “stop me whenever that one goes away for any reason” behaviour, BindsTo= is the setting to read up on.

Further reading: systemd.unit(5) manual page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link