How to run you program on Raspberry Pi at start up
There are many ways to make a program run on Raspberry Pi at start up.
This is especially useful when running your Pi headless (without a monitor or screen of any kind). The most common way to achieve this would be to add a cron job, but there are a few other solutions that work in different ways as well.
Here we will look at what I found to be the simplest. Simply add one single line or very small block of code to the rc.local file. Find the full tutorial on how to do this just below.
Firstly always make sure that your Pi software is up to date to ensure all the newest libraries are available.
In the Terminal simply type to update:
sudo apt-get update && sudo apt-get upgrade
Then make your script executable:
chmod 755 myscript.py
Now that the Raspberry Pi has all the latest software you can open your rc.local file, type the following into your terminal:
sudo nano /etc/rc.local
This line opens a folder that is situated in the system that you can now edit and save from within your terminal. Now that the desired folder is open you can enter your command that will tell your Raspberry Pi to start the programs, just before the exit 0 line.
python /home/pi/myscript.py
NB. Do not forget to ensure that the exit line is there. This ensures that after running your command the Pi goes back and runs the normal boot process. Otherwise it will get stuck in an endless loop and your SD card might need to be reformatted. Similarly the ‘&’ sign at the end of your command is just as important.
Resources
https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/