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:
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 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 &
Save and exit with ctrl + x, followed by y when prompted to save, and then enter. Reboot your Pi with:
sudo reboot
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
Save and exit with ctrl + x, followed by y when prompted to save, and then enter. Reboot your Pi with:
sudo reboot
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
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
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?
 
 
Crontab @reboot is also another easy method too!
Was about to say the same thing. I like having all the scheduled programs in the same place. Nightly stuff, @reboot stuff, all in crontab.
Good to know there's lots of love for crontab. I'm always curious to know what people use!
I use it to call periodic logging events for sensors or check in logs to servers. Great for iot projects! Allows your code to be very tight for the sensing without worrying about the scheduler or task overhead
For the benefit of people who aren't familiar with crontab, it schedules things to happen at specified times. It's mostly used for things that need to happen periodically (great for datalogging!), though the crontab file can be rather arcane. (One little bit of advice: rather than running a file directly, I like to use a "caller" program which is usually a short script that in turn runs the file I actually want to run. This technique sidesteps the need to remember to re-run crontab every time I make changes in the program that I actually want to run.)
Related sidenote: If you want something to happen just once at a particular time in the future, see the Linux at command.
One other thing: If you have problems with @reboot stuff coming up before all of the other stuff is done, stick in a sleep statement into your script to delay your program a few seconds, e.g., "sleep 15" will give an extra 15 seconds.
Valuable guide! Much obliged to you for composing it, Shawn. Do you happen to realize how to have any of these strategies run a python program as a root client? My program requires here association with One Wire gadgets and I trust it can just do as such when I run the program with sudo.
Useful guide! Thank you for writing it, Shawn. Do you happen to know how to have any one of these methods run a python program as a root user? My program requires interaction with One Wire devices and I believe it can only do so when I run the program with sudo.
Of the ones in the guide, rc.local and systemd run as root, so you should be able to run your Python program as root with those. Some of the other methods might (for example, you'd need to use the root user's crontab if you want to use crontab).
Very helpful, very clear. Thank you. Haven't tried it!... but will be my first port of call when I need this. I particularly liked the setting out of the options, complete with pros an cons.
One question, Shawn: Although I've done several projects using RPis, I haven't done any (yet) where I'd like to have the display NOT turn off after a period of no user activity (such as a mouse movement or keyboard click). A simple example would be the "clock display" you mention. If you know how to do this off hand, it could save me some time trying to dig it up!
I have not tested this, but it looks like you can add the following to
/home/pi/.config/lxsession/LXDE-pi/autostart
:Source forum thread
Thanks, Shawn! I'm working on a presentation which I'm planning on running from a Zero connected to the projector in order to convince the audience that it's a "noticable" computer... having it "go blank" while I answer a question is not "good form".
Aaaah...that would be most unfortunate. What do you mean "noticable?" Is this a workshop?
Not really a "workshop" -- it's a presentation aimed at (non-"techie") senior citizens who've been hearing the words "micro:bit", "Arduino", and "Raspberry Pi" from grandkids (and others) and don't have a clue what they are. (Or why they should consider them as Christmas/Hannukah presents!)