Search code examples
pythonraspberry-pistartupautostart

Difficulty executing Python on boot


I am trying to get this python file with an infinite loop on my raspberry pi to start on boot and stay running while the pi has power. I've tried editing my "etc/rc.local" file and adding the different command variations below. I am certainly referencing the right file and path. I also added a 10 second delay at the beginning of the scrip to make sure everything is in order before it runs. If anyone has any input that'd be great, this is seemingly a common task and I dont know why I am having difficulty.

Additionally I dont have a shebang line at the top of my python script. I dont know if that has anything to do with my problem even though i am specifying the file is a python file.

/usr/bin/python3 /home/pi/Desktop/webplants/mqtt.py & sudo /usr/bin/python3 /home/pi/Desktop/webplants/mqtt.py & sudo python3 /home/pi/Desktop/webplants/mqtt.py & python3 /home/pi/Desktop/webplants/mqtt.py &


Solution

  • You should use systemd. This utility will easily allow you to run a python script on startup. This tutorial should cover it: https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/#systemd