I've created the following systemd unit:
[Unit]
Description=MagicMirrorConfig
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=sh /home/pi/magicmirror-config/launcher.sh
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Note after looking at various examples of how to set this up, I've added both an After
and Wants
for network-online.target
.
launcher.sh
will start a Python script which attempts to send various MQTT messages.
Following a reboot if I check the systemctl journal I can see the following error:
Dec 12 12:53:21 magicmirror sh[640]: Traceback (most recent call last):
Dec 12 12:53:21 magicmirror sh[640]: File "/home/pi/magicmirror-config/pir.py", line 37, in <module>
Dec 12 12:53:21 magicmirror sh[640]: client.connect(broker)
Dec 12 12:53:21 magicmirror sh[640]: File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 914, in connect
Dec 12 12:53:21 magicmirror sh[640]: return self.reconnect()
Dec 12 12:53:21 magicmirror sh[640]: File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1044, in reconnect
Dec 12 12:53:21 magicmirror sh[640]: sock = self._create_socket_connection()
Dec 12 12:53:21 magicmirror sh[640]: File "/home/pi/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
Dec 12 12:53:21 magicmirror sh[640]: return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
Dec 12 12:53:21 magicmirror sh[640]: File "/usr/lib/python3.9/socket.py", line 843, in create_connection
Dec 12 12:53:21 magicmirror sh[640]: raise err
Dec 12 12:53:21 magicmirror sh[640]: File "/usr/lib/python3.9/socket.py", line 831, in create_connection
Dec 12 12:53:21 magicmirror sh[640]: sock.connect(sa)
Dec 12 12:53:21 magicmirror sh[640]: OSError: [Errno 101] Network is unreachable
This makes me think that the network is not available when my service starts.
Is there a way I can stop my service from starting until the network is available?