Wie bekomme ich es hin das eine Python Datei Automatisch gestartet wird auf dem Raspberry Pi5?

Ich habe Raspberry PiOS 64-Bit und versuche schon lange wie ich meine Python Datei "Autostarten" kann. Das Script schaut in einem Loop nach MQTT Nachrichten und führt befehle aus, wenn eine Bestimmte Nachricht eintrifft. Daher braucht es ja auch Internet zugriff usw. .

Ich habe schon vieles versucht wie "crontab -e" und eine Zeile mit "@reboot python3 /home/user/Openhab_Mqttv2.py &" hinzugefügt hat aber nicht geklappt und jetzt sitze ich daran es mit "systemd" zu probieren. Jedoch klappt es auch nicht und ich kriege nur Fehlermeldungen. Ich verstehe auch nicht ganz wie das "systemd" funktioniert. Außer dass ich eine ".service" Datei in "/etc/systemd/system" brauche damit etwas ausgeführt wird.

sudo nano /etc/systemd/system/Openhab_Mqttv2_Autostart.service

[Unit]
Description=Openhab Mqtt working  Autostart
After=network.target


[Service]
TimeoutStartSec=2min
ExecStart=/home/richard/Openhab_Mqttv2.py
Restart=always


[Install]
WantedBy=multi-user.target

Status:

x@raspberrypi:~ $ systemctl status Openhab_Mqttv2_Autostart.service
× Openhab_Mqttv2_Autostart.service - Openhab Mqtt working  Autostart
     Loaded: loaded (/etc/systemd/system/Openhab_Mqttv2_Autostart.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Tue 2024-04-02 23:51:00 CEST; 5s ago
   Duration: 1ms
    Process: 75239 ExecStart=/home/richard/Openhab_Mqttv2.py (code=exited, status=203/EXEC)
   Main PID: 75239 (code=exited, status=203/EXEC)
        CPU: 1ms


Apr 02 23:51:00 raspberrypi systemd[1]: Openhab_Mqttv2_Autostart.service: Scheduled restart job, restart counter is at 5.
Apr 02 23:51:00 raspberrypi systemd[1]: Stopped Openhab_Mqttv2_Autostart.service - Openhab Mqtt working  Autostart.
Apr 02 23:51:00 raspberrypi systemd[1]: Openhab_Mqttv2_Autostart.service: Start request repeated too quickly.
Apr 02 23:51:00 raspberrypi systemd[1]: Openhab_Mqttv2_Autostart.service: Failed with result 'exit-code'.
Apr 02 23:51:00 raspberrypi systemd[1]: Failed to start Openhab_Mqttv2_Autostart.service - Openhab Mqtt working  Autostart. 

journalctl:

x@raspberrypi:~ $ sudo journalctl -u Openhab_Mqttv2_Autostart.service
Apr 02 21:49:44 raspberrypi systemd[1]: Started Openhab_Mqttv2_Autostart.service - Mein Service.
Apr 02 21:49:45 raspberrypi python3[958]: Traceback (most recent call last):
Apr 02 21:49:45 raspberrypi python3[958]:   File "/home/richard/Openhab_Mqttv2.py", line 417, in <module>
Apr 02 21:49:45 raspberrypi python3[958]:     client.connect( "192.168.0.182", 1883, 60)
Apr 02 21:49:45 raspberrypi python3[958]:   File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 914, in connect
Apr 02 21:49:45 raspberrypi python3[958]:     return self.reconnect()
Apr 02 21:49:45 raspberrypi python3[958]:            ^^^^^^^^^^^^^^^^
Apr 02 21:49:45 raspberrypi python3[958]:   File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 1044, in reconnect
Apr 02 21:49:45 raspberrypi python3[958]:     sock = self._create_socket_connection()
Apr 02 21:49:45 raspberrypi python3[958]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Apr 02 21:49:45 raspberrypi python3[958]:   File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
Apr 02 21:49:45 raspberrypi python3[958]:     return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
Apr 02 21:49:45 raspberrypi python3[958]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Apr 02 21:49:45 raspberrypi python3[958]:   File "/usr/lib/python3.11/socket.py", line 851, in create_connection
Apr 02 21:49:45 raspberrypi python3[958]:     raise exceptions[0]
Apr 02 21:49:45 raspberrypi python3[958]:   File "/usr/lib/python3.11/socket.py", line 836, in create_connection
Apr 02 21:49:45 raspberrypi python3[958]:     sock.connect(sa)
Apr 02 21:49:45 raspberrypi python3[958]: OSError: [Errno 101] Network is unreachable
Apr 02 21:49:45 raspberrypi systemd[1]: Openhab_Mqttv2_Autostart.service: Main process exited, code=exited, status=1/FAILURE
Apr 02 21:49:45 raspberrypi systemd[1]: Openhab_Mqttv2_Autostart.service: Failed with result 'exit-code'.
Apr 02 21:49:45 raspberrypi systemd[1]: Openhab_Mqttv2_Autostart.service: Scheduled restart job, restart counter is at 1.

Was genau kann ich an dem "/etc/systemd/system/Openhab_Mqttv2_Autostart.service" File ändern damit es endlich klappt, dass die Python Datei im Loop läuft und was genau habe ich falsch gemacht?

Mit ChatGPT versucht und es hat mir für die ".service" Datei gesagt, dass ich auch "/usr/bin/python3" zwischen "ExecStart=" und "/home" hinzufügen sollte. Habe es versucht und in der Fehlermeldung steht drin, dass man nicht mehr als einen setting in "ExecStart=" rein machen darf und auch mehrere "ExecStart=" gehen auch nicht.

Linux, Boot, System, Programmiersprache, Python, Autostart, Python 3, Raspberry Pi

Meistgelesene Fragen zum Thema Raspberry Pi