检测apache与nginx的80端口并自动重启的脚本,感兴趣的朋友可以参考下。
先来看apache的监测脚本。
复制代码代码如下:
#!/bin/bash
IP=`ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk -F ' ' '{print $2}'| awk -F':' '{print $2}'| head -n 1` \\获取IP 地址 echo "">/root/jiankong_log/apache_tmp.log DATE=`date +%Y-%m-%d" "%H:%M` netstat -ntl | awk '{print $4}' | grep ":80$" //检测是否监听80 if [ $? != 0 ];then //如果没有监听80 则执行 echo "$DATE apache is error, Try to starting...." >>/root/jiankong_log/apache.log >>/root/jiankong_log/apache_tmp.log /usr/local/apache2.2.0/bin/httpd -k start //启动apache netstat -ntl | awk '{print $4}' | grep ":80$" //检验80 if [ $? == 0 ];then //如果启动成功 记录日志 echo "$DATE apache start ok" >>/root/jiankong_log/apache.log else //如果启动失败 记录日志 并邮件通知 echo "$DATE apache start ERROR, will send mail to administrator" >>/root/jiankong_log/apache.log >>/root/jiankong_log/apache_tmp.log /usr/local/mutt/bin/mutt -s ""$IP"-apache" 281450202@qq.com -c 1516070****@139.com </root/jiankong_log/apache_tmp.log && echo "$DATE send OK" >>/root/jiankong_log/apache.log fi else echo "$DATE nothing" >>/root/jiankong_log/apache.log // 没有任何异常,统计用。 fi
下面这个是监测nginx 80端口的脚本。
复制代码代码如下:
#!/bin/bash
IP=`ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk -F ' ' '{print $2}'| awk -F':' '{print $2}'| head -n 1` echo "">/root/jiankong_log/nginx_tmp.log DATE=`date +%Y-%m-%d" "%H:%M` netstat -ntl | awk '{print $4}' | grep ":80$" if [ $? != 0 ];then echo "$DATE nginx is error, Try to starting...." >>/root/jiankong_log/nginx.log >>/root/jiankong_log/nginx_tmp.log /usr/local/nginx/sbin/nginx netstat -ntl | awk '{print $4}' | grep ":80$" if [ $? == 0 ];then echo "$DATE nginx start ok" >>/root/jiankong_log/nginx.log else echo "$DATE nginx start ERROR, will send mail to administrator" >>/root/jiankong_log/nginx.log >>/root/jiankong_log/nginx_tmp.log /usr/local/mutt/bin/mutt -s ""$IP"-nginx" 281450202@qq.com -c 1516070****@139.com </root/jiankong_log/nginx_tmp.log && echo "$DATE send OK" >>/root/jiankong_log/nginx.log fi else echo "$DATE nothing" >>/root/jiankong_log/nginx.log fi
脚本很简单,不过功能还是可以的,放在计划任务里面每隔5分钟执行一次。防止网站挂了,你还不知道。只要能解决问题,方法不怕山寨。
原作者:李坤山 |