echo "now start to check net 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
url=${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 and reboot
if [ $connected -eq 0 ];then
echo "network is not very well !"
echo "now restart net !"
/etc/init.d/networking restart
/sbin/ifup wlan0
/sbin/ifup eth0
fi
注意 if 条件判断的方括号“["、”]" 左右两边都有空格,要执行这个脚本需用到curl 工具:
sudo apt-get install curl
Ctrl + O保存,Ctrl+X退出
给net_restart.sh添加可执行权限:
sudo chmod +x /etc/network/if-down.d/net_restart.sh
同理,新建 net_reboot.sh脚本,net_reboot.sh脚本用于当网络断开时重启树莓派:
#!/bin/bash
echo "check net is OK or not!"
echo "if not,then will reboot"
echo "bash file in /etc/network/if-down.d/net_reboot.sh"
#check net is conneted or not
for ((i=0; i < $count; i++))
do
url=${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 and reboot
if [ $connected -eq 0 ];then
echo "network is not very well or not connected !"
echo "now start to reboot !"
#/etc/init.d/networking restart
#/sbin/ifup wlan0
#do
#echo "now reboot"
killall motion #这里添加重启pi前要执行的任务,比如杀死某些你启动的进程,需自己修改