-1

I have a Raspberry Pi and run a server on it (Python Script). I would like the server to restart when it goes down. I did a little googling and found out that the easiest way to do this is by using the systemd service units and the option Restart=always. So I created a service for my python script and started it. Everything worked fine.

But here is my problem, when I restart the Raspberry Pi, the service is running too early and the server (Python script) cannot assign the correct IP address. It only uses the local-host IP address 127.0.0.1 (The server must be accessible by other computers on the network).

In the best case the service should also wait until a GSM module connected to the Raspberry Pi is started.

My question is now is there a possibility that the service waits for the right IP address to be available and maybe even waits until the GSM module is initialized?

I already tried to create a .socket file for the service but I didn't seem to know how to set it up correctly.

Hopefully you guys can help me :)

Krupp J
  • 23
  • 4
  • I’m voting to close this question because From the tag: systemd questions should be for *programming questions* using systemd or its libraries. Questions about *configuring the daemon* (including writing unit files) are better directed to Unix & Linux: https://unix.stackexchange.com. Please delete this. – Rob Jul 17 '23 at 07:06

1 Answers1

1

You could to try to add the following lines to the [Unit] section in your systemd service file:

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

network.target makes sure that the network service has been started.

network-online.target makes sure that an Internet connection can be established

aurx
  • 48
  • 5
  • I have tested it, but the server still only listens on 127.0.0.1, so it seems that the service still does not wait until the Raspberry Pi has an IP address. Do you have any other ideas what I could try? – Krupp J Jul 16 '23 at 17:41
  • 1
    hm... maybe add `ExecStartPre=/bin/sleep x` and x for the seconds to wait until `ExecStart` will be called in the `[Service]` section. That way you would just wait x seconds before your server starts. – aurx Jul 16 '23 at 19:04
  • Perfekt thank you that helped alot! – Krupp J Jul 17 '23 at 09:38