Skip to main content
2 of 3
added 328 characters in body
Craver2000
  • 141
  • 2
  • 10

Using systemd to prevent python scripts from crashing

I want to prevent multiple python scripts from crashing on my Raspberry Pi (Stretch OS).

I wanted to clarify if this would be the way of implementing a systemd file to prevent multiple python scripts from crashing. Under the service section of the service file, the typical format is:

[Service]
ExecStart=/path/too/script
Restart=always

But since I am running multiple python scripts, I think I should add python to the front of the path, as well as stacking service section on top of each other. Please correct me if I am wrong.

My current script (constantrun.service) is:

[Unit]
Description='python scripts that needs to be run constantly'

[Service]
ExecStart=python /home/pi/projects/script1.py
Restart=always
[Service]
ExecStart=python /home/pi/projects/script2.py
Restart=always
[Service]
ExecStart=python /home/pi/projects/script3.py
Restart=always

[Install]
WantedBy=multi-user.target

However, when I run try to start this service file with sudo systemctl start constantrun.service. I get the following error:

Failed to start constantrun.service: Unit constantrun.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status constantrun.service' for details.

I open the log, and I see:

● constantrun.service - 'python scripts that needs to be run constantly'
   Loaded: error (Reason: Invalid argument)
   Active: inactive (dead)

Feb 18 17:15:12 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:11] Executable path is not absolute, ignoring: python /home/pi/T
Feb 18 17:15:12 raspberrypi systemd[1]: constantrun.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Feb 18 17:20:17 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:5] Executable path is not absolute, ignoring: python /home/pi/TE
Feb 18 17:20:17 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:8] Executable path is not absolute, ignoring: python /home/pi/TE
Feb 18 17:20:17 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:11] Executable path is not absolute, ignoring: python /home/pi/T
Feb 18 17:20:17 raspberrypi systemd[1]: constantrun.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Feb 18 17:20:33 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:5] Executable path is not absolute, ignoring: python /home/pi/TE
Feb 18 17:20:33 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:8] Executable path is not absolute, ignoring: python /home/pi/TE
Feb 18 17:20:33 raspberrypi systemd[1]: [/lib/systemd/system/constantrun.service:11] Executable path is not absolute, ignoring: python /home/pi/T
Feb 18 17:20:33 raspberrypi systemd[1]: constantrun.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

How can I modify the code above to get it working? Also, do I need to add a path to my python library such asPYTHONPATH=/home/pi/.local/lib/python2.7/site-packages?

I am really new to the linux environment, and would appreciate any advice on this!

Craver2000
  • 141
  • 2
  • 10