发布于 2017-06-05 12:06:44 | 137 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Linux常用命令,程序狗速度看过来!

Linux

Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。


这篇文章主要介绍了Linux 检测服务器是否连接着网络的相关资料,需要的朋友可以参考下

Linux 检测服务器是否连接着网络

摘要: 每隔5分钟检测一次服务器是否连接着网络,如果三次检测都没有网络?则自动关机! 主要使用场景: 由于自己有一台服务器放在偏远的老家,有可能会遇到停电导致断网的问题,并且停电后UPS使用时间也有限制, 因此设计此脚本为了解决停电的时候服务器突然断电引起的各种问题,当停电后网络也就不通了,此时需要自动关闭服务器. 当然,来电后需要手动启动服务器!!!


 #!/bin/bash

# 检测服务器是否连接着网络,如果网络不通 则 3次后 关机
# crontab -e
# */5 * * * * ./check.sh

echo "Starting test network was clear..."

if test -e ./checkInfo
 then
 echo "CheckInfo File Exist..."
else
 cat /dev/null > ./checkInfo
fi

last_res=`head -1 ./checkInfo`

checkInternet(){
 ping_res=1

 for url in "8.8.8.8" "61.139.2.69" "114.114.114.114" "168.95.1.1" "223.5.5.5" "180.76.76.76"
 do
 echo "PING ${url}"

 ping=`ping -c 3 ${url}|awk 'NR==7 {print $4}'`

 if [ ${ping} -eq 0 ]
  then
  ping_res=1
  else
  ping_res=2
 fi

 if [ ${ping_res} -eq 2 ]
  then
  break
 fi
 done

 return ${ping_res}
}

checkInternet

result="$?"

if [ ${result} -eq 1 ]
 then
 if [ "${last_res}" = "1" ]
  then
  echo "2" > ./checkInfo
 elif [ "${last_res}" = "2" ]
  then
  cat /dev/null > ./checkInfo
  init 0
  else
  echo "1" > ./checkInfo
 fi
 else
 cat /dev/null > ./checkInfo
fi

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务