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.
This Raspberry Pi tutorial will be requiring a few parts:
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.
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 in this tutorial, 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!
10 thoughts on “Running a Python Program on boot with the Raspberry Pi”
Nik Rowland
Be aware that there seems to be a limit to the number of characters allowed in the path/filename string in the line added to rc.local. It is somewhere between 29 and 45 in that 29 works for me and 45 doesn’t. This had me stumped for months. I use Python 3. Thinking back to DOS and shortening the file name did the trick.
In your example line,
“python (/home/pi/weather.py) &”
the string between the brackets is where the character count is critical.
Always test your line for correct function in a Terminal window. If it works there it will work in rc.local. If it doesn’t work in a Terminal window, then at least you will get an error message. In my case ‘unable to find file’ error meant the string is too long.
Chris @ BCR
Nik – interesting point! We haven’t ever come across that issue but testing in a terminal window is always very good advice!
katunziv
thanks, but what if i want to run a script on start up and the scripts prints some outputs which i should see them on the terminal, what can i do
john
this method wont worked if u have input() on your code
Nik Rowland
katunziv,
Have you tried arranging for your Pi to boot into a CLI (a terminal window) rather than the Desktop. You should see your printed outputs then.
To do this go :-
‘Applications Menu’>’Preferences’>’Raspberry Pi Configuration’ and in the ‘Systems’ window change from ‘Boot to Desktop’ to ‘Boot to CLI’ and reboot.
When you’ve finished and wish to return to the Desktop just type ‘startx’. You can then re-edit the configuration and reboot again.
Robert Macmillan
This is helpful. How do I “compile” my python program, or otherwise hide the source code from the user?
Thank you
Brad
Hi. I am pretty new to this. I am using a Pi zero, and the newest release of the Raspberry Pi operating system. My code runs in a terminal window without a problem. I followed your directions exactly. It seems the code loads to a certain extent. LED flashes as it should, but I get no response from my WiFi keypad. I have tried several other methods as well to run code on boot, mostly with the same result. Any info would be greatly appreciated. Thanx.
Oliver
Hi Chris, thanks for this very clear description.
The setup is very straightforward with only one py script running on a clean OS.
The script is a very simple MQTT application, which is running fine when launched from the CLI.
$ python home/pi/mqtt_app.py &
It doesn’t require any input or output from the CLI, it’s just running in the background, waiting for MQTT pub to activate GPIOs.
But something seems to prevent the script to launch, can’t figure out. It’s driving me nuts !
Jordan
Does this work with a python virtual environment? I can’t get it to work on my raspberry pi zero 2 w
mike87
Python changing things every few years to ensure 90% of tutorials on the internet don’t work…