Systemd – Start Service After Network Fully Connected

startupsystemd

Background

I am writing a simple script to be running in Raspbain on Raspberry Pi 2, simple turn a LED on to indicate I am ready to connect with SSH from my desktop computer.

The script is not important here, only to mention that as I use frequence control so the script is running an infinite loop, to turn the LED on and off frequently. So this is an example of simple service. However, at least the accepted answer of this question advise me to set the type to idle. So my service file looks like

[Unit]
Description=Turn on LED after SSH is ready

[Service]
Type=idle
ExecStart=/usr/bin/sshready.py

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

Effect

The service runs as expected. However, I noticed when I start putty in my desktop computer right after the LED turns on the login prompt not appear immidiately. So I checked with

$ systemd-analyze plot > output.svg

The result shows

enter image description here

Question

It looks like my services starts not after network-online.target, what is wrong here and how can I fix it?

Best Answer

When there is a question about a systemd directive, you can use man systemd.directives to find where it's documented. In this case it shows that After= is documented in man systemd.unit.

In that file, it shows that After= directive is listed in the "[UNIT] SECTION OPTIONS", indicating that it belongs in the [Unit] section of the file.

The same documentation also documents the [INSTALL] section options, and After= is not listed there.

In short, your After= directive was in the wrong location the unit file so it had no effect until you moved it to the correct location.

Related Question