Raspberry-7*24小时持续保持网络连接
在使用RASPBERRY的过程中,当ssh断开连接一段时间后,RASPBERRY会自动断开与网络的连接。
RASPBERRY:使用无线网卡连接到路由器
pi@raspberry ~ $ lsmod
8192cu //无线网卡型号 一: 解决网络不稳定问题——RASPBERRY断线重连 + thought
-
定时监测网络的连通性
ping “www.baidu.com” “cn.bing.com”
-
if(1) true do nothing ;else restart network
-
if(2) true do nothing ;eles reboot RASPBERRY(this part hasn’t finished)
-
将1.2.3加入定时任务
-
script
1.install tools
sudo apt-get curl //script 中用到了curl
2.sudo vi /etc/network/if-down.d/net_restart.sh
wirte codes in the file of net_restart.sh #!/bin/bash #value define urls=("www.baidu.com" "www.2345.com/?kuwait") http_code=("200" "301" "302" "404") count=${#urls[*]} connected=0 echo "now start to check net-connected is on or not!" echo "bash file in /etc/network/if-down.d/net_restart.sh" #check net is conneted or not for ((i=0;i < $count; i++)) do urls=${urls[$i]} result=$(curl -o /dev/null -s -m 10 -w %{http_code} $url) for flag in ${http_code[*]} do if [ $flag = $result ];then connected=$(expr $connected + 1) fi done done #if net is down then restart if [ $connected -eq 0 ];then echo "network is bad" echo "restart it" /etc/init.d/networking restart /sbin /ifup wlan0 /sbin/ifup eth0 fi
3.添加执行权限
sudo chmod +x /etc/network/if-down.d/net_restart.sh
4.test this script
sudo /etc/network/if-down.d/net_restart.sh sudo ip link set eth0 down && sudo ip link set wlan0 down&& sudo /etc/network/if-down.d/net_restart.sh
5.添加定时任务
sudo crontab -e //write this code */20 * * * * /etc/network/if-down.d/net_restart.sh #restart cron make it works sudo /etc/init.d/cron restart ---
二:RASPBERRY电源计划管理
经过断线重连的配置发现有时ssh还是不能登陆,当每20分钟执行一次脚本后ssh就能够ssh登陆,后在论坛找到是无线网卡的问题,当一段时间不使用之后,raspberry会对无线网卡进行休眠。
-
查看无线网卡是否启用了电源管理
cat /sys/module/8192cu/parameters/rtw_power_mgnt 如果返回结果为**1**表明启用了电源管理
-
禁用电源管理
sudo vi /etc/modprobe.d/8192cu.conf //write the code in the end of this file
#Disable power saving options 8192cu rtw_power_mgnt=0
sudo reboot