Exécuter au démarrage.

How to run a program when starting the Raspberry Pi?

Updated the 27 August 2020 - 0 Comments - , , , ,

ⓘ This article may have been partially or fully translated using automatic tools. We apologize for any errors this may cause.

Sometimes we need to run a program or a command line when our Raspberry Pi starts up.

This is particularly the case when we manufacture an object for home automation, and we want to automatically launch a program when the object is connected.

In this tutorial we will see a very simple way to launch a program when starting the Raspberry.

The material we are going to use.

In this tutorial we only need:

If you want to complete this tutorial without being able to directly access the Raspberry Pi, you will also need a PC that can read SD cards or use an adapter.

Start a program when the Raspberry Pi starts up with rc.local.

For Mac OS and Windows you will need to be able to access the rootfs partition if you want to do this from your PC. To do this, see our tutorial to read a Linux partition from Mac and Windows.

The easiest way to launch a program when starting the Raspberry Pi is to use the file /etc/rc.local.

This file contains a set of commands that will be executed just before the Raspberry Pi has finished booting.

To launch a program when the Raspberry Pi starts up, you just need to modify the file /etc/rc.local on the SD card (from the Pi, or by modifying the file /etc/rc.local in the score rootfs from the card connected to your PC).

To start the program, just look for the line:

exit 0

in the file and add the command line calling your program before the line exit.

For example, if I want to call a Python 3 script named example.py and located in the folder /home/pi on startup, I will replace the exit 0 line with:

/usr/bin/python3 /home/pi/example.py

Some important points should be noted.

First point, the program will be executed by the user root and will therefore have all the rights. Be careful what you are doing!

Second point, you should always use absolute and non-relative paths, the behavior of relative paths is unpredictable.

Third and last point, your program must give back control to the script or the Raspberry Pi will never be able to finish booting. If your program performs an infinite loop, you must run it in the background by adding a & after ordering. In our case this would give:

/usr/bin/python3 /home/pi/example.py &

Your program will now be correctly launched when starting the Raspberry Pi!

Share
Newsletter
Author:
Raspberry Pi FR
Join the Raspberry Pi community
A $35 computer already sold 10 million copies
BUY THE NEW RASPBERRY PI 4
Related tutorials
Create a Wi-Fi hotspot in less than 10 minutes with Pi Raspberry! Install Android on his Raspberry Pi How to install a web server on the Raspberry Pi (Apache + PHP + MySQL) Create a NAS with your Raspberry Pi and Samba How to recover the password of your Raspberry Pi if you lost it. 2018 Update.
No Comments
Share
Newsletter Sign-Up
Subscribe to the newsletter.
¿Te gustó este artículo?
¡Suscríbete y mantente informado!