Raspberry Pi Startup Scripts: Run Raspberry Pi Programs on Boot

Publié par

Raspberry Pi SBCs (single-board computers) are popular devices for a wide variety of uses. There are many instances where you want a process to run every time it boots up; the last thing you want to have to do is start it yourself. The good news is, you don't need to.

Three ways to start a program on boot are explained here, with slightly different uses. The crontab method is especially useful, as it not only starts a program upon bootup, but it can also run things at a set interval after bootup. Let's look at three methods to set up your programs to run automatically via the command line or SSH (Secure Shell).

Testing Setup

Let's assume you're working from the command line, or logging into your Pi via SSH.

For this setup, install an LED on BCM pin 14 (aka physical pin 8 for testing) directly or via a transistor. I've written test programs similar to the blink.py routine below, which blinks the LED twice. This is accomplished by navigating to the home directory (cd /home if needed) then typing sudo nano blink.py. Enter the program below and hit ctrl+x to exit and save.

Raspberry Pi Startup Scripts Image 3

To test your setup, add lines as shown to the bottom of the text file. * * * * * will run a command every minute, which in my case is the test program: python3 /home/blink.py. Additionally, cron can run a program every time on boot by using @reboot instead of numbers or stars for the time. The @reboot python3 /home/blink3.py command will run a slightly modified blink3.py program (which blinks three times so you can tell which routine is running) each time it reboots.

bashrc Raspberry Pi

Raspberry Pi Startup Scripts Image 4

Raspberry Pi Startup Scripts Image 5

While not as versatile as crontab, bashrc has a unique feature ­— it runs on boot and every time a new terminal is opened or you log on via SSH. That can be useful in some situations. To try it out, type in sudo nano /home/pi/.bashrc. You'll be taken to a text editor containing a variety of commands.

On a new line at the bottom of the file, simply add your commands, such as python3 /home/blink3.py. There's no need for anything else; just hit ctrl+x to exit, and y then enter to save the file. Restart your Pi with sudo reboot now then log in to see the results. Note that this script won't run until you log in, which means you must enter your password and username on SSH or via the terminal; however, it will automatically occur if booting into the desktop environment.

Test your script out by entering source ~/.bashrc to run the file. You can even add echo commands to confirm that the routine is run on your terminal, as shown in the above images.

Raspberry Pi AutoStart rc.local

Raspberry Pi Startup Scripts Image 6

Similar in function to bashrc, you can add functions to rc.local to have them run on bootup. Note that rc.local isn't dependent upon you logging in. To edit, enter sudo nano /etc/rc.local into the terminal, then insert the program you want to run before the exit 0 text. As seen below, I've entered python3 /home/blink3.py &. The ampersand here allows other processes to execute while the program is executing. If your rc.local script gets stuck, your boot sequence will not proceed, so be cautious about how you implement this functionality.

Conclusion

Raspberry Pi systems, and Linux in general, give you a variety of ways to automate running programs. Whether you want to turn on a light at a certain time, back up your hard drive every night, or run a heavily modified plant-watering Keurig, you can get your trusty Pi-based assistant to handle it for you!

 

Actualité

Sorry, your filter selection returned no results.

Nous avons mis à jour notre politique de confidentialité. Prenez un moment pour lire les changements. En cliquant sur "J'accepte", vous acceptez la clause de confidentialité d'Arrow Electronics ainsi que les conditions d'utilisation.

Notre site Internet place des cookies sur votre appareil pour améliorer votre expérience et pour améliorer notre site. Pour en savoir plus sur les cookies que nous utilisons et la façon de les désactiver, cliquez ici. Des cookies et des technologies de suivi peuvent être utilisés à des fins de marketing. En cliquant sur « Accepter », vous consentez au placement de cookies sur votre appareil et à notre utilisation de technologies de suivi. Cliquez sur « En savoir plus » pour de plus amples informations et instructions sur la façon de désactiver les cookies et les technologies de suivi. Même si l'acceptation des cookies et technologies de suivi est volontaire, leur désactivation peut entraîner un mauvais fonctionnement du site Internet et certaines publicités peuvent être moins pertinentes pour vous. Nous respectons votre confidentialité. Lisez notre politique de confidentialité ici.