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: systemctl try-restart only restarts a service that is already running

Systemctl
~/tips/tip-systemctl-try-restart

sudo systemctl try-restart nginx.service

Apply an update in a script without starting services you had stopped.

A deploy script that runs restart will also start a service you stopped on purpose. There is a verb that leaves stopped things alone.

sudo systemctl try-restart nginx.service

The man page describes it as stopping and then starting the units if they are running. When the service is not running, nothing happens and the command still succeeds.

That behaviour suits maintenance scripts. After renewing a certificate or updating a library, restart whatever uses it, and skip what is switched off on this particular machine:

sudo systemctl try-restart nginx.service postfix.service dovecot.service

I like it on a fleet where not every host runs every service, because the same script can go everywhere without a pile of if statements.

The flip side is that it will not help you when the service has crashed. A failed unit is not running, so try-restart skips it and your script reports success. If the goal is a running service afterwards, use restart and check the result with is-active. Also note that a restart drops open connections, even when only the certificate changed, so a reload can be kinder when the program supports it.

Further reading: systemctl man page

More Systemctl tips sudo systemctl daemon-reload systemctl list-units --type=service --state=running systemctl -H [email protected] status nginx sudo systemctl reboot --firmware-setup systemctl list-jobs All Systemctl tips (9) All topics

Link