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: Wants= plus After=network-online.target makes a service wait for a working network

Linux
~/tips/tip-systemd-network-online-target

After=network-online.target

Makes a service start only once the network is really up, not just started.

Plenty of unit files on the internet say After=network.target and assume the service now waits for the network. It doesn’t, really, and I think that one line explains a good share of the “works when I restart it by hand, fails at boot” complaints.

network.target is a passive unit. It says little more than “the network management stack has started”, so it barely delays anything. A service that needs to reach another host at startup (mount a share, or talk to a database on 192.168.1.x) should use network-online.target. And it needs two lines, because After= only sets the order. It does not pull the target in. Wants= does that:

sudo systemctl edit myapp.service

[Unit]
Wants=network-online.target
After=network-online.target

Save the drop-in and the next boot starts myapp.service only after the network management software reports the network as up. What “up” means depends on the wait service that belongs to your setup: systemd-networkd-wait-online.service with systemd-networkd, NetworkManager-wait-online.service with NetworkManager. They are enabled along with their main service, but only used once something wants network-online.target. You can see which one is active with:

systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service

The caveat is boot time. The target can add a real delay and times out after 90 seconds, so I’d only add it to services that consume something over the network. A daemon that just listens for others, like a web server, doesn’t need it; the systemd docs say the same.

Further reading: systemd.special(7) manual page, Running services after the network is up (systemd.io)

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