我的Python脚本在启动RPI大约5分钟后退出
我编写了一个小python脚本,检查是否有东西从我的raspberry pi连接到了特定的IP地址,然后更改了3个GPIO引脚中的一个引脚的值,以打开连接、未连接或连接错误的指示灯。该代码在rpi启动时运行,在最初的4.5-5分钟内运行良好,然后在代码中的任何一点都会被卡住我的Python脚本在启动RPI大约5分钟后退出,python,raspberry-pi,Python,Raspberry Pi,我编写了一个小python脚本,检查是否有东西从我的raspberry pi连接到了特定的IP地址,然后更改了3个GPIO引脚中的一个引脚的值,以打开连接、未连接或连接错误的指示灯。该代码在rpi启动时运行,在最初的4.5-5分钟内运行良好,然后在代码中的任何一点都会被卡住 #!/usr/bin/env python # /etc/init.d/ping.py ### BEGIN INIT INFO # Provides: ping.py # Required-Start: $rem
#!/usr/bin/env python
# /etc/init.d/ping.py
### BEGIN INIT INFO
# Provides: ping.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Descripton: Enable service provided by daemon.
### END INIT INFO
import subprocess
import RPi.GPIO as GPIO
# Set up pins, address, GPIO
address = '10.101.60.131'
grn = 8
red = 10
yel = 12
GPIO.setmode(GPIO.BOARD)
GPIO.setup(grn, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(yel, GPIO.OUT)
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.LOW)
last = 5
# main loop, checks if network is there
while(1):
ret = subprocess.call(['ping', '-c', '3', address])
if (ret != last):
# network responded correctly
if ret == 0:
print('ping to ' + address + ' OK')
GPIO.output(grn, GPIO.HIGH)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.LOW)
last = 0
# network was not located
elif ret == 2:
print('no response from ', address)
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.HIGH)
GPIO.output(yel, GPIO.LOW)
last = 2
# other error in setup
else:
print('ping to ', address, ' failed')
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.HIGH)
last = 3
如果我在启动之外运行代码(在python空闲状态下启动),它运行得很好。如果需要更多信息,请告诉我。我解决这个问题的方法是从
/etc/profile
运行python代码。在文件末尾的/etc/profile中,我添加了sudopython/home/pi/Desktop/filenamehere.py&
,然后保存并重新启动 您是从systemd
开始的吗?我认为脚本通常应该在后台启动其他东西,然后在90秒左右的时间内退出。我在这个网站上使用了方法3,其中包括将脚本设置为在启动部分的init.d文件中运行。我认为你被那篇文章误导了。查看此页面上的service.sh
,您应该响应参数start
和stop
和restart
,并且您应该只执行相对短暂的操作,例如在后台启动Python脚本,然后退出。因此,基本上我可以运行一些短暂的操作。解决方法是使用脚本启动python程序吗?是的,没错。将脚本存储到其他地方,然后执行python/some/place/else/YourScript.py&