Language: EN

como-ejecutar-aplicacion-al-arranque-de-una-raspberry-pi

Options to Run an Application at Boot of a Raspberry Pi

Learning to automatically run applications at boot is very important in a wide range of Raspberry Pi projects.

For example, you may want to start a web server, a custom application, or a script. Or even connect to a physical device, such as a screen, a sensor, or an actuator.

There are several options to configure your Raspberry Pi to automatically run these applications every time it starts. Here is a table with different options to achieve this.

MethodDescription
rc.localSimple method but less flexible
systemdProvides more detailed control
AutostartPerfect for graphical applications in the desktop environment
cronGood option for simple scripts and scheduled tasks

As it could not be otherwise (otherwise the world would be very easy), each of these methods has its own advantages and disadvantages. In this article, we will explore the different options available.

Use rc.local

To configure the rc.local file and run an application at boot, we start by opening the /etc/rc.local file 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 that 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 check that the command runs correctly using the command:

sudo reboot

Use systemd

systemd is the init and service management system used in Raspberry Pi OS. It provides a more modern and flexible method to run applications at 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 my_application, we use the following command:

sudo nano /etc/systemd/system/my_application.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 at boot:

sudo systemctl start my_application
sudo systemctl enable my_application

Finally, we check the status of the service to ensure it is running correctly:

sudo systemctl status my_application

Add 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 the autostart, we start by creating an entry file in the ~/.config/autostart/ directory. For example, for an application called my_application, we use the following commands:

mkdir -p ~/.config/autostart
nano ~/.config/autostart/my_application.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 reboot the desktop environment or the Raspberry Pi to apply the changes. We use the command:

sudo reboot

Use Cron

The cron command allows you to schedule tasks, and with the @reboot option, you can run scripts at boot.

To configure cron, we first open the current user’s crontab file using the following command:

crontab -e

Inside the crontab file, we add a line to run our script at 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