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 reload-or-restart reloads a service when it can and restarts when it cannot

Linux
~/tips/tip-systemctl-reload-or-restart

sudo systemctl reload-or-restart nginx.service

Reloads a service when it supports reloading and does a full restart when it does not.

After editing a config file I used to stop and think about whether the service supports a reload. systemd can make that choice for you.

sudo systemctl reload-or-restart nginx.service

The man page says it reloads the unit if it supports that, and otherwise stops and starts it. A reload asks the running process to re-read its configuration. A restart is the heavier option, since the process goes away and comes back, which you may want to avoid on a busy service.

That lets you use one command in notes or scripts for many services without remembering which of them define a reload action. A unit that has none falls back to the restart.

One detail from the docs: if the unit is not running yet, it gets started. When you do not want that, use try-reload-or-restart, which does the same job but does nothing when the unit is not running:

sudo systemctl try-reload-or-restart nginx.service

The command does not check your configuration first. A broken config combined with a restart fallback can leave the service stopped. For nginx I run the config test before touching the service:

sudo nginx -t && sudo systemctl reload-or-restart nginx.service

The && makes the second command run only when the test passes.

Further reading: systemd: systemctl man page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link