The best tools to make your project dreams come true

Login or Signup

3/25/2019 | By Maker.io Staff

Running Python Programs on Startup Using the Raspberry PI

Up until now, all of our Raspberry Pi Python programs have been executed by either using the IDLE or via console window. However, we may want our program to run on boot automagically and in this How-To we will learn how to do it on Raspbian!

Why have a script run on start-up?

Most computer users will be familiar with user interface-based programs that require user input to perform an action. For example, a game takes user keyboard and mouse data that manipulate a virtual character to run, jump, swim, and crawl. Other applications (such as office programs) contain interactive elements that perform functions like saving documents, creating new files, executing external scripts, and sending packets over the internet. However, these types of programs extensively interact with their users and often rely on the user launching the program.

Programs written for microcontrollers, however, are often concerned with controlling hardware and performing more basic functions. While these programs can interact with users, they require execution as soon as the system is booted. The Raspberry Pi is arguably a fusion between a microcontroller and PC as it provides the hardware functions of a microcontroller (GPIO for example) while also establishing an architecture that can run Raspbian (a distribution of Linux). Because of this, many users use the Raspberry Pi in projects as a microcontroller and therefore require their programs to execute as soon as the Pi is booted. This ensures so that you do not need to remotely access the Pi and run the program.

RC.local

The first task in getting a Python program to boot on start-up is to create a script file that call the Python program to execute. Copy and paste the following code into a file and then save that file as “startup.sh”. To keep things simple, make sure that the script file is saved to your Documents folder.

Copy Code
#!/bin/sh
sleep 10
sudo python yourPythonProgram.py

If you are unsure of how to create a script file then to do so open a terminal window and then launch nano. To launch nano use the following commands below:

Copy Code
cd Documents
nano startup.sh

When you are done editing the file, press Ctrl + X and this will prompt you if you want to save the file. Enter Y and the next option will ask you for the file name. Since we have already specified the file just press enter and the file will be created under Documents.

The first line of the script file informs the system that the script should be run with bash as opposed to executed in another shell. The second line adds a small delay, and this is very important to include. Users of Raspbian have reported that if a Python script is executed too soon after start-up, some services may not be available, such as serial ports. The third line is the command that will call your Python program and this should also be in Documents. If you want to execute a Python program in a different folder, then ensure that the correct path is defined.

This script, however, will not be called on boot by itself, therefore we need to edit a special file on the Raspbian OS, RC.local. In a console window, use the following command to open the rc.local file in nano:

Copy Code
sudo nano /etc/rc.local

Once in nano, you will need to enter the following line:

Copy Code
/home/Documents/startup.sh & 

The ‘&’ symbol at the end is crucial as it executes the script in a separate thread. If this symbol is missing then the system runs the script in the main system thread. If the script does not end (i.e., an infinite loop), then the system never boots. Once the file has been saved, your Pi should now execute your Python program on start-up!