How to Run a Script at Boot on Raspberry Pi
Make your Python script or app run every time the Raspberry Pi starts up.
There are a lot of reasons you’d want to run a Python script, an app or another type of script (ex: a Bash script) every time your Raspberry Pi boots up. Perhaps you have a robot or IoT device that has to be ready to perform a task as soon as the Raspberry Pi powering it starts up. Or maybe you just want to have a particular program running in the background at all times and don’t want to have to launch it manually at every session.
There are a number of ways to automatically start a script at Raspberry Pi bootup, but the easiest is to use crontab, a scheduling feature that also lets you set scripts to run at particular times.
How to Run a Script at Raspberry Pi Boot
1. Edit your crontab list by typing:
sudo crontab -e
You can launch crontab without entering sudo, but if you do, you won’t be able to run scripts that require admin privileges. In fact, you get a different list of crontabs if you don’t use sudo so don’t forget to keep using it or not using it.
2. Select nano if you are prompted to ask for an editor.
A file opens.
3. Add a line at the end of the file that reads like this:
Stay On the Cutting Edge: Get the Tom's Hardware Newsletter
Get Tom's Hardware's best news and in-depth reviews, straight to your inbox.
@reboot python3 /home/pi/myscript.py
The line has to begin with @reboot which tells it to run every time you boot the Raspberry Pi. If it’s a Python script, you’ll want to put the command to launch the python or python3 interpreter followed by the full path to your Python script.
If it is a Bash script or another app, just put the full path to it.
4. Save and exit. In nano, you do that by hitting CTRL + X, answering Y and hitting Enter when prompted.
5. Make your script executable if it is a Bash script. Python scripts won’t need to be executable because the python interpreter already is. You can make any script executable by typing
sudo chmod a+x FILENAME
If you want to remove your script from the crontab, simply type sudo crontab -e again and remove or comment out that line. Note that, if you are building a project that doesn’t require you to use the windowed environment, you can save system resources by configuring the Raspberry Pi to boot to the command line by entering sudo raspi-config and then navigating to Boot Options -> Desktop / CLI and selecting Console Autologin.
How to Autorun a Script or App in the Raspberry Pi GUI
If you want to have your script or app run at Raspberry Pi startup within the windowed GUI, follow these steps.
1. Create a file called myapp.desktop (or something else .desktop) in the /etc/xdg/autostart/ directory.
sudo nano /etc/xdg/autostart/myapp.desktop
2. Use the following layout in the myapp.desktop file.
[Desktop Entry]
Exec=chromium-browser https://www.tomshardware.com
Put the command and any parameters on the Exec= line. For example, for a Chrome browser to open to a web page, you’d put “chromium-browser [URL].” If your app requires sudo permissions, you can put sudo in the Exec command.
To run a script in a terminal window, use lxterminal followed by the --command parameter and double quotes with “/bin/bash -c ‘MYCOMMANDS HERE; /bin/bash”’. For example, to launch a python3 script that requires sudo permissions, you’d use:
Exec=lxterminal --command”/bin/bash -c ‘sudo python3 /home/pi/myscript.py; /bin/bash’”
That will launch a terminal window in the windowed environment upon boot with your script running in it. Once the script finishes (or you abort it by hitting CTRL+C), you the terminal window will return to the prompt. If you want the terminal window to close itself upon completion of the script, you can leave out the ;/bin/bash at the end.
Running a script in a terminal window like this can be helpful, because if it’s a Python script on endless loop (such as you’d have for a robot), you can easily kill the script by hitting CTRL+C. Otherwise, to kill the script, you’d need to find the process, which we describe below.
Killing the Script
What if you want to stop your autorunning script after your Raspberry Pi has booted? If your script has already completed running, it will be gone from memory but if it’s designed to do something continuously, you’ll need to search for and kill the task.
1. Search for your script by using the ps aux command and putting the name of your script (or at least a partial name) after grep.
ps aux | grep app.py
Replace app.py with the name of your script. You will see a list of process numbers.
2. Kill each process number using the sudo kill -9 command. For example,
sudo kill -9 437
sudo kill -9 438
More Tutorials:
- How to Capture Screenshots on Raspberry Pi
- How to Set Up a Raspberry Pi Web Server
- How to Run Raspberry Pi 4 or 3 Off an SSD or Flash Drive
- How to Create Custom Keyboard Shortcuts on the Raspberry Pi
- How to Run Emulators on the Raspberry Pi 4
- 25+ Linux Commands Raspberry Pi Users Need to Know
- How to Make a News Ticker Shirt With Raspberry Pi
-
bit_user In fairness, I didn't know crontab had @reboot. However, I wonder if it can distinguish between a real reboot and the cron daemon simply getting restarted. Probably doesn't matter... just a thought.Reply
What if you want to stop your autorunning script after your Raspberry Pi has booted? If your script has already completed running, it will be gone from memory but if it’s designed to do something continuously, you’ll need to search for and kill the task.
That's when you really ought to consider making it a service. See:
https://www.raspberrypi.org/forums/viewtopic.php?t=266701https://domoticproject.com/creating-raspberry-pi-service/Anyway, on killing a process...
1. Search for your script by using the ps aux command and putting the name of your script (or at least a partial name) after grep.
...
2. Kill each process number using the sudo kill -9 command.
You can do it in just one step, using the killall command. Just be sure your script is named uniquely, and that you really want to kill all instances of it.
Probably should also mention something about signals and how it's sometimes with trying SIGINT or SIGTERM, before reaching for SIGKILL.