本帖最后由 yegeziz 于 2017-10-7 08:59 编辑
网上有很多方案,下面我列出我最顺手的防范.
原理:通过在init.d下创建服务,通过服务来启动脚本
1.创建服务.
sudo nano /etc/init.d/fan
创建服务文件"fan"
fan内容:
#!/bin/bash
#!/bin/bash
### BEGIN INIT INFO
# Provides: any
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: auto-fan service
# Description: auto-fan service daemon
### END INIT INFO
case "$1" in
start)
echo -n "Starting python: "
echo "---------------------------------------------------------------------------------" >>/var/log/python
date +"! %T %a %D : Starting python ." >>/var/log/python
echo "---------------------------------------------------------------------------------" >>/var/log/python
echo start
python /home/pi/scp/fan.py &
#sh /root/finmonAgent/startpy
echo "Done."
echo ""
echo "---------------------------------------------------------------------------------" >>/var/log/python
date +"! %T %a %D : Finished." >>/var/log/python
echo "---------------------------------------------------------------------------------" >>/var/log/python
touch /var/lock/subsys/python
;;
stop)
echo -n "Shutting Down python Listeners: "
echo "---------------------------------------------------------------------------------" >>/var/log/python
date +"! %T %a %D : Shutting Down python." >>/var/log/python
echo "---------------------------------------------------------------------------------" >>/var/log/python
echo "python"
killall -9 finmonAgent.py
echo "Done."
rm -f /var/lock/subsys/python
echo "Done."
echo "---------------------------------------------------------------------------------" >>/var/log/python
date +"! %T %a %D : Finished." >>/var/log/python
echo "---------------------------------------------------------------------------------" >>/var/log/python
;;
*)
echo "Usage: python { start | stop | restart }"
exit 1
esac
exit 0
ctrl+o保存
ctrl+x退出
2.重新载入服务
systemctl daemon-reload
启动服务
停止服务
重启服务
systemctl restart fan
查询服务状态
systemctl status fan
设置开机启动
systemctl enable fan
2.2方案BBBBB
sudo chmod 755 /etc/init.d/homebridge
sudo update-rc.d homebridge defaults
sudo reboot重启就成功了。
[/hide]
3.顺便保存自动风扇的脚本
import RPi.GPIO as GPIO
import time
import commands
T_HIGH = 52
T_LOW = 40
fan_pin = 40
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fan_pin, GPIO.OUT)
def get_gpu_temp():
gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )
return float(gpu_temp)
GPIO.output(fan_pin, 1)#第一次通电,风扇默认开转.
while 1:
gpu_temp_loop = get_gpu_temp()
print 'CPU temp:', gpu_temp_loop,'C'
if gpu_temp_loop > T_HIGH:
GPIO.output(fan_pin, 1)
elif gpu_temp_loop < T_LOW:
GPIO.output(fan_pin, 0)
time.sleep(5)
|