我将一个带有无限循环的python脚本放入/etc/rc.local
但机器成功启动,这让我感到困惑。
内容/etc/rc.local
:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
/home/pi/py/startsignal.py &
/home/pi/py/fan.py
touch /home/pi/thisisrun
exit 0
启动信号.py
#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, 1)
风扇.py
#!/usr/bin/python
# coding: utf8
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
upper_temp = 55
lower_temp = 45
# minutes
check_interval = 2
def get_temp():
with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f:
temp = float(f.read()) / 1000
return temp
def check_temp():
if get_temp() > upper_temp:
gpio.setup(23, gpio.OUT)
elif get_temp() < lower_temp:
gpio.setup(23, gpio.IN)
if __name__ == '__main__':
# check every 2 minutes
try:
while True:
check_temp()
sleep(check_interval * 60)
finally:
gpio.cleanup()
所有相关的代码都在上面。谷歌搜索后我想到了这个。
- 表示
#!/bin/sh -e
一旦发生错误脚本将退出。 - 文件没有创建,所以这
/home/pi/thisisrun
行上面一定有错误 - 启动到系统后,我可以看到它
fan.py
正在运行。所以我猜错误发生在fan.py
. 但是fan.py
里面有一个无限循环!
python脚本如何产生错误但仍然正常运行?
当永远不会返回时如何/bin/sh
检测错误?fan.py
操作系统:树莓派拉伸