Run a Raspberry Pi Program on Boot

If you’ve used a microcontroller (such as an Arduino), you probably realize that there is beauty in its simplicity. When you give it power, it (almost always) begins running your code right away without any hassle! The same simplicity does not hold for full-fledged computers, like a Raspberry Pi.

Because systems like the Pi run an operating system (Linux), many things must occur in the background before you’re able to run your own code. While general purpose operating systems are extremely powerful and offer a lot of flexibility to users, it requires extra effort to tell the system that it should run your program right after booting.

To give you a few ways to run your program on boot, we have a new tutorial for you:

New!

How to Run a Raspberry Pi Program on Startup

September 18, 2018

In this tutorial, we look at various methods for running a script or program automatically whenever your Raspberry Pi (or other Linux computer) boots up.

In it, we show three methods for scheduling a Python program to run right after startup along with some troubleshooting tips in case it doesn’t work on the first try. While the tutorial is more thorough, below is the abbreviated, TL;DR version.

rc.local

rc.local is likely the easiest method for running your program on boot, but because it executes before any graphical desktop starts, you will not have access to graphical elements. To add your program (we’ll use an example Python 3 program named blink.py), modify rc.local:

sudo nano /etc/rc.local

Just before the exit 0 line, add the following:

/usr/bin/python3 /home/pi/blink.py &

Running a Python script on boot with rc.local on the Raspberry Pi

Save and exit with ctrl + x, followed by y when prompted to save, and then enter. Reboot your Pi with:

sudo reboot

autostart

If you need access to graphical elements (for example, you are making a dashboard with Tkinter), you will need to wait until the X Window System has started before running your program. The easiest way to do that on the Pi is to use the autostart system (which is included with LXDE, the graphical desktop environment in Raspbian).

autostart runs a script located at /home/pi/.config/lxsession/LXDE-pi/autostart for your user (pi) each time the desktop environment is started. It then looks for .desktop files located in /home/pi/.config/autostart to run. To use autostart, we’ll make our own .desktop file with our arbitrary blink.py program.

In a terminal, enter the following commands:

mkdir /home/pi/.config/autostart
nano /home/pi/.config/autostart/blink.dekstop

In the blink.desktop file, enter the following:

[Desktop Entry]
Type=Application
Name=Blink
Exec=/usr/bin/python3 /home/pi/blink.py

Using autostart to run a program on boot in Linux

Save and exit with ctrl + x, followed by y when prompted to save, and then enter. Reboot your Pi with:

sudo reboot

systemd

systemd is a more robust way of creating services to run your programs, but it is more complicated to use. While it is intended to start programs in the background, independent of any user-level desktop environments, you can still create unit files for systemd that wait until networking, graphics, etc. or just brute force restarts until the program runs (see the systemd section in the full tutorial for more information).

To create a basic unit file, run the following:

sudo nano /lib/systemd/system/blink.service

Enter the following into the blank .service document:

[Unit]
Description=Blink my LED
After=multi-user.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/blink.py

[Install]
WantedBy=multi-user.target

Run a program on boot with systemd on the Raspberry Pi

Save and exit with ctrl + x, followed by y when prompted to save, and then enter. We then need to tell systemd to recognize our new service and enable it with the following:

sudo systemctl daemon-reload
sudo systemctl enable blink.service

Finally, reboot your system with:

sudo reboot

Conclusion

Because it’s Linux, there are many ways to accomplish a thing, and starting a program on boot is no exception. Other methods exist, including SysVinit and crontab. What is your favorite method for running a program on startup with Linux?

comments | comment feed

Leave a Reply

Your email address will not be published. Required fields are marked *