Raspberry Pi Startup Scripts: Run Raspberry Pi Programs on Boot

게시자

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!

 

최신 뉴스

Sorry, your filter selection returned no results.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.