连续4个小时ping不通远端主机,则本机关机

 1 #!/bin/bash
 2 #连续4个小时ping不通远端主机,则本机关机。
 3 
 4 begin_time=$(date "+%s")
 5 while true;do
 6     sleep 2
 7     ping -c 1 172.16.160.1 > /dev/null
 8     if [ $? != 0 ];then
 9         end_time=$(date "+%s")
10     else
11         begin_time=$(date "+%s")
12     fi
13     if [ $end_time -gt $begin_time ] ; then
14         time_difference=$(($end_time - $begin_time))
15         echo $time_difference
16         if [ $time_difference -gt 14400 ]; then
17         shutdown -h now
18         fi
19     fi
20 done

 

posted @ 2017-10-31 18:39  wangmo  阅读(688)  评论(0编辑  收藏  举报