cfanbo / cfanbo.github.io

1 stars 0 forks source link

一个自动检测服务器网络状态的脚本 | 学习笔记 #242

Open cfanbo opened 10 months ago

cfanbo commented 10 months ago

https://blog.haohtml.com/archives/11889/

最近服务器总是莫明奇妙的网络不通。我在上一篇文章里写了一个自动检测网卡状态的脚本: http://blog.haohtml.com/archives/11827.但发现服务器仍然出现此问题.但服务器内部运行正常.这里基本可以确认是与外网不通的原因.就写了一个脚本,用来检测网络状态,在不通的情况就重启服务器了.此解决办法相当的变态.最好不要使用. 1.创建新文件 /data/check_net_status.sh,内容如下

!/bin/bash if [ $# -ne 1 ] then echo 'ip address not defined!' exit fi ip=$1 log_file="$(date +%Y%m%d).log" ret=ping -c 10 $ip | grep ttl | wc -l if [ $ret -lt 2 ]; then echo "$(date +%H:%M:%S) ===== reboot" >> $log_file shutdown -r now fi 添加执行权限

chmod +x /data/check_net_status.sh 2.创建定时检测shell脚本,实现5分钟自动检查是否与网关的连接状态,如果不通,则自动重启本服务器. 编辑/etc/crontab文件在现有行 /2 root /data/check_net.sh > /dev/null 后面新添加一行 /5 root /data/check_net_status.