How to run scripts on boot on the Raspberry Pi

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.

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.

How to run scripts on boot on the Raspberry Pi Picture 1How to run scripts on boot on the Raspberry Pi Picture 1 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.

How to run scripts on boot on the Raspberry Pi Picture 2How to run scripts on boot on the Raspberry Pi Picture 2 Add a line at the end of the file

4. Save and exit. In Nano, you do it by pressing CTRL+ X, reply Yand press Enterwhen 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 -eand 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-configand 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.

How to run scripts on boot on the Raspberry Pi Picture 3How to run scripts on boot on the Raspberry Pi Picture 3 Place the command and any parameters on the Exec = line

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 auxand 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
4 ★ | 66 Vote