Learning how to run applications automatically on boot is a very important aspect in a large number of Raspberry Pi projects.
For example, because you want to start a web server, a custom application, or a script. Or even connect to a physical device, like a screen, a sensor, an actuator.
There are several options to configure your Raspberry Pi to run these applications automatically every time it starts. Here is a table with different options to achieve this.
| Method | Description |
|---|---|
rc.local | Simple method but less flexible |
systemd | Offers more detailed control |
| Autostart | Perfect for graphical applications in the desktop environment |
cron | Good option for simple scripts and scheduled tasks |
As could not be otherwise (otherwise the world would be too easy) each of these methods has its own advantages and disadvantages. In this article, we will explore the different available options.
Using rc.local
To configure the rc.local file and run an application on boot, we start by opening the file /etc/rc.local in a text editor with superuser permissions using the following command:
sudo nano /etc/rc.local
Inside the file, before the line exit 0, we add the command to start our application or script. For example, to run a Python script, we add:
/usr/bin/python3 /home/pi/miscripts/miscript.py &
It is important to add the & at the end of the command to ensure the process runs in the background.
After adding the command, we save the file and close the editor using Ctrl+X, then Y, and press Enter.
Finally, we reboot our Raspberry Pi to verify that the command runs correctly using the command:
sudo reboot
Using systemd
systemd is the system startup and service manager used in Raspberry Pi OS. It provides a more modern and flexible method for running applications on boot.
To configure a service with systemd, we start by creating a new service file in /etc/systemd/system/. For example, for a service called mi_aplicacion, we use the following command:
sudo nano /etc/systemd/system/mi_aplicacion.service
Inside the file, we enter the following configuration:
[Unit]
Description=My Application
[Service]
ExecStart=/usr/bin/python3 /home/pi/miscripts/miscript.py
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
- ExecStart: Path to the executable or script.
- Restart: Configuration to restart the service if it fails.
- User: User that will run the service.
After adding the configuration, we save the file and close the editor.
For systemd to recognize the new service, we reload the configuration with the following command:
sudo systemctl daemon-reload
Then, we start the service and enable it to start on boot:
sudo systemctl start mi_aplicacion sudo systemctl enable mi_aplicacion
Finally, we check the service status to ensure it is running correctly:
sudo systemctl status mi_aplicacion
Adding the script to the autostart directory
This method is useful for graphical applications and scripts that need to start in the desktop environment.
To configure autostart, we start by creating a desktop entry file in the ~/.config/autostart/ directory. For example, for an application called mi_aplicacion, we use the following commands:
mkdir -p ~/.config/autostart nano ~/.config/autostart/mi_aplicacion.desktop
Inside the file, we enter the following configuration:
[Desktop Entry]
Type=Application
Exec=/usr/bin/python3 /home/pi/miscripts/miscript.py
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=My Application
Comment=Starts my application at boot
After adding the configuration, we save the file and close the editor using Ctrl+X, then Y, and press Enter.
Finally, we restart the desktop environment or the Raspberry Pi to apply the changes. We use the command:
sudo reboot
Using Cron
The cron command allows scheduling tasks, and with the @reboot option, you can run scripts on boot.
To configure cron, first we open the crontab file for the current user using the following command:
crontab -e
Inside the crontab file, we add a line to run our script on boot. For example, we can add:
@reboot /usr/bin/python3 /home/pi/miscripts/miscript.py
After adding the entry, we save the file and close the editor using Ctrl+X, then Y, and press Enter.
Finally, we reboot our Raspberry Pi to verify that the script runs correctly. We use the command:
sudo reboot

