Linux 檢測服務器是否連接著網(wǎng)絡
Linux 檢測服務器是否連接著網(wǎng)絡
摘要: 每隔5分鐘檢測一次服務器是否連接著網(wǎng)絡,如果三次檢測都沒有網(wǎng)絡?則自動關(guān)機! 主要使用場景: 由于自己有一臺服務器放在偏遠的老家,有可能會遇到停電導致斷網(wǎng)的問題,并且停電后UPS使用時間也有限制, 因此設計此腳本為了解決停電的時候服務器突然斷電引起的各種問題,當停電后網(wǎng)絡也就不通了,此時需要自動關(guān)閉服務器. 當然,來電后需要手動啟動服務器!!!
#!/bin/bash
# 檢測服務器是否連接著網(wǎng)絡,如果網(wǎng)絡不通 則 3次后 關(guān)機
# 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
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Linux系統(tǒng)中 /etc/fstab 文件的深入解讀
這篇文章主要給大家介紹了Linux系統(tǒng)中 /etc/fstab 文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01
關(guān)于Read-only file system問題的解決
這篇文章主要介紹了關(guān)于Read-only file system問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
CentOS7 修改網(wǎng)卡名稱為eth0&在VMWare中添加多網(wǎng)卡配置
這篇文章主要介紹了CentOS7 修改網(wǎng)卡名稱為eth0&在VMWare中添加多網(wǎng)卡配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Linux文件操作新手指南之關(guān)于install命令的用法
這篇文章主要介紹了Linux文件操作新手指南之關(guān)于install命令的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02

