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.

A 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.

4. 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.

To 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
- 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
May be interested
- How to start Raspberry Pi 3 from USBraspberry pi is a great, versatile software suite, with a variety of capabilities like running a media center or using as a radio.
- Why should people try Raspberry Pi 4?the new raspberry pi 4 has been released, but do you need to upgrade? is raspberry pi 4 really offering more features than the previous raspberry pi 3 b +?
- Raspberry Pi AI Kit: Bring AI to Raspberry Pithe raspberry pi ai kit is essentially raspberry pi's new m.2 hat+ board integrated with the hailo-8l acceleration module.
- Learn Pi Imager, How to Use Raspberry Pi Imagerthe raspberry pi foundation recently released a new application for recording sd cards for the raspberry pi. the new program, called raspberry pi imager, launches march 5, 2020.
- How to use Raspberry Pi Imager to install Raspberry Pi OSraspberry pi foundation recently released a new app for burning sd cards for raspberry pi. the new program, called raspberry pi imager, launched on march 5, 2020.
- 5 ways to make good use of Raspberry Pi 4the latest raspberry pi 4 has the strongest hardware version, opening up completely new categories of what you can do with the application.
- Is Thonny or Visual Studio Code better for Raspberry Pi users?both thonny and visual studio code are built into raspberry pi os. both are capable of running python scripts and are useful when you want to get things working using the raspberry pi.
- How to automate Photoshop with Photoshop Scriptstoday, this article will show you how to use photoshop scripts. it gives you the ability to change the behavior of a running script - something that photoshop actions can't do!
- 7 best Raspberry Pi 4 casesalthough the raspberry pi 4 is quite small, that doesn't mean you shouldn't equip it with an appropriate 'home'. here are the 7 best raspberry pi 4 cases you can buy right now.
- How to set up Raspberry Piyou may need some extra hardware and software for many of these projects, but the raspberry pi is still a great base.