How to run scripts on boot on the Raspberry Pi
There are many reasons why you want to run Python scripts, applications, or other types of scripts (e.g., Bash scripts) every time the Raspberry Pi boots. For example, you have a robot or IoT device that must be ready to perform a task as soon as the Raspberry Pi starts up. Or maybe you just want to have a specific program run in the background all the time and don't want to manually launch each session.
There are several ways to automatically start scripts when you boot up a Raspberry Pi, but the easiest way is to use crontab, a scheduling feature that also allows you to set scripts to run at specific times.
How to run scripts when the Raspberry Pi boots
1. Edit the crontab list by typing:
sudo crontab -e
You can start crontab without typing sudo, but if you do, you won't be able to run scripts that require admin rights. In fact, you get a list of other crontab if you're not using sudo, so don't forget to use it.
2. Select nano if you are asked about the editor.
Select nano if you are asked about the editorA file will open.
3. Add a line at the end of the file with the following content:
@reboot python3 /home/pi/myscript.py
This line must start with @reboot , it will run every time you boot Raspberry Pi. If it's a Python script, you'll want to place the command to launch the python or python3 interpreter , followed by the full path to the Python script.
If it's a Bash script or other application, just put the full path to it.
Add a line at the end of the file4. Save and exit. In Nano, you do it by pressing CTRL
+ X
, reply Y
and press Enter
when prompted.
5. Make the script executable if it is a Bash script. Python scripts will not need to be executed because there is python interpreter. You can make any script executable by typing:
sudo chmod a+x FILENAME
If you want to delete your script from crontab, just retype sudo crontab -e
and delete or note the line. Note that, if you are building a project that does not require a windowed environment, you can save system resources by configuring the Raspberry Pi to boot into the command line by typing sudo raspi-config
and then adjusting. Navigate to Boot Options> Desktop / CLI and select Console Autologin.
How to automatically run scripts or applications in the Raspberry Pi GUI
If you want to run scripts or applications when starting the Raspberry Pi in the GUI, follow these steps.
1. Create a file named myapp.desktop (or something else .desktop) in / etc / xdg / autostart /.
sudo nano /etc/xdg/autostart/myapp.desktop
2. Use the following layout in myapp.desktop file .
[Desktop Entry] Exec=chromium-browser https://www.tomshardware.com
Place the command and any parameters on the Exec = line . For example, for Chrome to open a web page, you need to enter 'chromium-browser [URL]'. If the application requires sudo permission, you can set sudo in the Exec command.
Place the command and any parameters on the Exec = lineTo run the script in the Terminal window, use lxterminal followed by the --command parameter and quotes with '/ bin / bash -c ' MYCOMMANDS HERE; / bin / bash ''. For example, to launch a python3 script that requires sudo privileges, you need to use:
Exec=lxterminal --command'/bin/bash -c 'sudo python3 /home/pi/myscript.py; /bin/bash''
That will launch a Terminal window in a windowed environment when booting with the script running in it. When the script is finished (or you cancel it by pressing CTRL
+ C
), the Terminal window will return to the prompt. If you want the Terminal window to close itself after completing the script, you can skip ; / bin / bash at the end.
Running a script in the Terminal window like this can be helpful, because if it has a Python script with an endless loop (such as you have a robot), you can easily end (kill) the script. by pressing CTRL
+ C
. On the other hand, to end the script, you have to find the process described below.
End of script
What if you want to stop the script from running automatically after the Raspberry Pi has booted? If the script has finished running, it will disappear from memory, but if it is designed to do something continuously, you will need to search and end the task.
1. Search for scripts using the command ps aux
and name the script (or at least part of the 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. End each process number with the command sudo kill -9
. For example:
sudo kill -9 437 sudo kill -9 438
You should read it
- How to add an ADC to Raspberry Pi: What you need to know
- Raspberry Pi Zero vs Model A and B, how are they different?
- What is the Raspberry Pi and how is the Raspberry Pi used?
- How to start Raspberry Pi 3 from USB
- Why should people try Raspberry Pi 4?
- Learn Pi Imager, How to Use Raspberry Pi Imager
- How to use Raspberry Pi Imager to install Raspberry Pi OS
- 5 ways to make good use of Raspberry Pi 4
- 7 best Raspberry Pi 4 cases
- How to set up Raspberry Pi
- What is the difference between Raspberry Pi 4 and other models?
- What's New in Raspberry Pi OS 11?
Maybe you are interested
Microsoft 365 Android PDF Viewer shows ads, even with subscription
This PowerShell script can bypass Windows 11 system requirements, Microsoft Account
How to execute Shell script in Linux
This is why people cancel their X Premium subscription!
Learn about Shockwave Unlimited: Subscription that replaces Game Pass and PS Plus
3 AI-powered video transcript creation tools to save hours of viewing