HomeTutorials › Running a Python Program on boot with the Raspberry Pi

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:

start

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

Step 1 – Get Everything Prepared

4

In this short tutorial, we are going to get started with the assumption that you have already set up your Raspberry Pi, installed Rasbian, 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

4

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

3

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

4

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

5

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

6

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!

Have A Question?
If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!

Parts Used In This Tutorial:

5 comments

  1. Nik Rowland
  2. katunziv
  3. john
  4. Nik Rowland

Leave a Reply