Running a Python Program on boot with the Raspberry Pi

One of the most common questions asked about the Raspberry Pi is how to get code you wrote to run automatically as soon as you boot it up. We get why this question is so common; a lot of people have entered this world of open electronics by way of the Arduino, and simply giving it power will start it up and run your code. With the Pi, there is a lot more going on so in this tutorial we will show you how to get your code up and running automatically as soon as the Pi starts.

There are a lot of different operating systems out there for the Raspberry Pi, so we are going to focus on the most popular: Raspbian. We are using the version dated: 2018-06-27 deviating from this may result in different / additional steps so if you are very new to this, we recommend following the tutorial exactly. Please Note: This will not work on the Lite version of Raspbian.

The Parts Needed: This Raspberry Pi 3 tutorial will be requiring a few parts:

1 x Raspberry Pi (3 or 3+ recommended)

1 x microSD card (with Raspbian 2018-06-27)

1 x Pi 3 / 3+ capable power supply

A Python Script that you would like to run on boot

Step 1 – Get Everything Prepared In this short tutorial, we are going to get started with the assumption that you have already set up your Raspberry Pi, installed Raspbian, and have a basic python program ready to go. So if you haven’t done any of that stuff, go ahead and get that sorted and we will continue on from there.

Step 2 – The Method There are numerous ways to accomplish this task of running a program on boot. We have picked the rc.local method as it seems to work for beginners with the least amount of hassle. We are going to edit a file called rc.local and add a command that will run our python program any time the Raspberry Pi boots up. You can add multiple commands to this file and run multiple programs – each one is run in order from top to bottom. Start by opening the terminal.

Step 3 – Open rc.local So how is it done? Start by opening the rc.local file by typing the following line into the terminal: sudo nano /etc/rc.local This will open the Nano text editor. The file has a little example code but we are going to remove that and write our own commands.

Step 4 – Clean Up The File Scroll down to the bottom using the arrow keys. We are going to delete the lines circled in the attached image. Be sure to keep the line “exit 0”

Step 5 -Add The Command Depending on where you saved your file and what you called it, your code will vary slightly. We called our file “weather.py” and saved it in the directory /home/pi/ so we are going to add this line above “exit 0”: python /home/pi/weather.py & It is important to note that we were using Python 2.7 to write our file, if you wrote your python file using Python 3, you will want to use “python3 /home/pi/startup.py &” You will also notice an ampersand at the end of the command. This is important if your python script is intended to keep running in the background indefinitely (or for a long period of time). The ampersand is used to fork the process and allow the operating system (and other programs) to continue to load. So if you have a simple script that is only supposed to run for a second or two when the Pi boots – leave it out. If you are running something like a datalogger or our weather station, you will definitely want it there!

Step 6 – Save and Reboot Once this is done, press “Ctrl + X” and then “Y” to save it. Now we should be able to reboot our Pi. Type into the terminal: sudo reboot That’s all there is to it! As the Pi reboots, your program should now run!