5

I'm strying to make my systemd service start after internet connection has been established. I read several questions on this forum, but no success so far. The error is analog to Invalid host: somedomain.net

However, running sudo systemctl restart myservice.service does make the service run succesfully. I'm using Raspbian om my RPI 3 B+.

The myservice.service file:

[Unit]
Description=my_service
After=network-online.target

[Service]
Type=simple
User=pi
ExecStart=/home/pi/service_bash_script

[Install]
WantedBy=multi-user.target

What am I missing?

Thanks in advance!

edit1

The script is simple and requires connection to it create a UDP stream. It's something like:

   #!/bin/sh
   aisdispatcher -r -d /dev/ttyS0 -s 38400 -H 192.168.1.5:5001
DA--
  • 223
  • 2
  • 6
  • The unit looks good but the service does not connect to the internet and as you say it works at expected. What we need is to know why your script does not connect to the internet. Can you please edit your question and add the script to it? Does the script runs on the command line? – Ingo Aug 16 '18 at 14:21
  • But maybe it does not wait for the connection to be established? Is my Unit valid for Raspbian? – DA-- Aug 16 '18 at 14:37
  • 1
    Have just answered. And yes your unit is valid for Raspbian. It is a general systemd unit and any distribution supporting systemd will work with it. Raspbian do it since Jessie. – Ingo Aug 16 '18 at 14:48

1 Answers1

6

network-online.target is a static unit. You can check it with:

rpi ~$ systemctl list-unit-files network-online.target
UNIT FILE             STATE
network-online.target static

1 unit files listed.

means it has no [Install] section and starts only if it is Wants or Requires by another unit. It cannot be started and stopped and does not run on bootup. But if it is not loaded then there is nothing to start After= and your unit will run without waiting for network-online. Add this statement:

Wants=network-online.target

to the [Unit] section and look if it fixes your problem.

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • See also https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ (which recommends the same thing as Ingo). The crux of the issue here is that After= does not make something a pre-requisite, it just means if this is in play, wait until after that. Wants= and Requires= create actual dependencies; see man systemd.unit (there is an index to all directives, BTW, in man systemd.directives, since they are spread across a number of man pages). – goldilocks Aug 16 '18 at 15:07
  • On a more sober note, this systemd stuff changes the way so many things are done. There are countless scripts offered online as solutions for this problem. These work of course, but as you've just shown, there are simpler and more elegant (?) ways to get the job done. – Seamus Aug 16 '18 at 16:37
  • addition to @goldilocks comment, https://www.freedesktop.org/software/systemd/man/systemd.directives.html – jsotola Aug 16 '18 at 21:49
  • 1
    @jsotola very helpful, haven't seen it before. But you have to take a little bit of attention. The documentation is for systemd 239. Raspbian get stuck on systemd 232 so there may be some issues documented which are not yet supported by Raspbian. If in doubt better to look at the man pages local. – Ingo Aug 16 '18 at 22:12
  • 1
    @Ingo, almost all linux man pages are online .... just google the command .... ie. man systemd.directives – jsotola Aug 16 '18 at 22:15