Start shell script on Raspberry Pi startup

对着背影说爱祢 提交于 2019-12-30 13:56:45

问题


I have a python program which posts to my local web server. The script runs on a raspberry pi running the latest version of raspbian 3.18. How can I make the Python script run at startup? Raspbian has a login password which is the first thing I have to remove. If the power ever goes out I want the pi to reboot and start running my script again. Should I be using Raspbian for this? The script is the only thing the pi is used for. I tried adding the script to /etc/init.d but I do not think it will run either way if the pi requires login info upon booting.


回答1:


Make sure the program is executable

chmod +x filename.py

open /etc/rc.local in your editor of choice

sudo nano /etc/rc.local

and add

python /FULL PATH TO SCRITP/filename.py

before the exit




回答2:


You can use the Linux crontab to run the Python script. Let's go to root and create a shell script.

sudo -i

nano startup.sh

Then type your python script on this bash script. navigate to home directory, then to this directory, then execute python script, then back home.

cd / cd home/pi/your directory sudo python yourpythonscript.py cd /

Save the script and then exit. Make this sh script executable by giving it permission.

chmod 755 startup.sh

Now open and edit rc.local file.

nano /etc/rc.local

Add /root/startup.sh & before exit 0

now save and exit from the file and reboot your pi.

sudo reboot



来源:https://stackoverflow.com/questions/30507243/start-shell-script-on-raspberry-pi-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!