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 enable --now starts a service and turns it on at boot in one go

Systemctl
~/tips/tip-systemd-systemctl-enable-now

sudo systemctl enable --now myapp.service

Enables a service for every boot and starts it right now with one command.

A lot of people run systemctl enable, see no error, and then wonder why nothing is listening. Enable only means “start this at boot”. It leaves the running system alone, and I find that a surprisingly easy thing to forget.

What enable does is create the symlinks from the [Install] section of the unit file (usually WantedBy=multi-user.target) and reload the systemd configuration. Starting is a separate job. Add --now and systemctl does both:

sudo systemctl enable --now myapp.service

The switch also works the other way round, and with mask and reenable. With disable --now the service is stopped right away and won’t come back after a reboot:

sudo systemctl disable --now myapp.service
systemctl is-enabled myapp.service

A typical case: you drop a new unit file in /etc/systemd/system/, run sudo systemctl daemon-reload, and then one enable --now gets it running today and after every boot. My advice is to make --now a habit, because the two-step version is exactly where people forget the second step.

One caveat. enable needs an [Install] section to have something to link. A unit without one shows up as static in systemctl is-enabled, and enabling it does nothing useful; it only runs when another unit pulls it in. Also, --now starts the service, it does not restart it. If it was already running and you changed the unit file, you still need a systemctl restart before the change takes effect.

Further reading: systemctl(1) manual 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