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.

请仔细阅读我们近期更改的隐私政策。当按下确认键时,您已了解并同意艾睿电子的隐私政策和用户协议。

本网站需使用cookies以改善用户您的体验并进一步改进我们的网站。此处阅读了解关于网站cookies的使用以及如何禁用cookies。网页cookies和追踪功能或許用于市场分析。当您按下同意按钮,您已经了解并同意在您的设备上接受cookies,并给予网站追踪权限。更多关于如何取消网站cookies及追踪的信息,请点击下方“阅读更多”。尽管同意启用cookies追踪与否取决用户意愿,取消网页cookies及追踪可能导致网站运作或显示异常,亦或导致相关推荐广告减少。

我们尊重您的隐私。请在此阅读我们的隐私政策。