Raspberry Pi Startup Scripts: Run Raspberry Pi Programs on Boot

Pubblicato da

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!

 

Ultime notizie

Sorry, your filter selection returned no results.

Non perderti le ultime novità sull'elettronica

Abbiamo aggiornato la nostra politica sulla privacy. Si prega di prendere un momento per rivedere questi cambiamenti. Cliccando su Accetto, l'utente accetta la Politica sulla privacy e Condizioni di utilizzo di Arrow Electronics.

Il nostro sito web mette i cookies sul vostro dispositivo per migliorare la vostra esperienza e il nostro sito. Leggete altre informazioni sui cookies che usiamo e su come disabilitarli qui. I cookies e le tecnologie di tracking possono essere usati per scopi commerciali.

Con un click su “Accept”, voi consentite l'inserimento dei cookies sul vostro dispositivo e l'uso da parte nostra di tecnologie di tracking. Per avere altre informazioni e istruzioni su come disabilitare i cookies e le tecnologie di tracking, clickate su “Read More” qui sotto. Mentre l'accettazione dei cookies e delle tecnologie di tracking è volontaria, una loro disabilitazione potrebbe determinare un funzionamento non corretto del sito web, ed alcuni messaggi di allarme potrebbero essere per voi meno importanti.

Noi rispettiamo la vostra privacy. Leggete qui la nostra politica relativa alla privacy