监控nginx 剧本,每20秒监测一次,若是历程数多于1000,重启nginx服务,重启5次失败后通知管理员,若是重启乐成一分种后,历程数照样大于1000,通知运维

#!/bin/bash
d=`date`
c=`pgrep -l nginx | wc -l`
check_nginx()
{
  for  ((i=1;i<=5;i++))
  do
        /usr/local/nginx/sbin/nginx   2> /tmp/nginx.log
        if [ $? -eq 0 ]
        then
                echo "${d}nginx 重启${i}次乐成" | mail -s "nginx 历程监控" 383xxxxx@qq.com
                break
        fi
  done
}
while :
do
        if [ c -gt 1000 ]
        then
                check_nginx
                if [ $i -eq 5 ]
                then
                        echo "${d}nginx 5次重启失败,请登录服务器处置" | mail  -s "nginx 历程监控" 383xxxxxx@qq.com
                        exit
                else
                        sleep 60
                        c1=`pgrep -l nginx | wc -l`
                        if [ c1 -gt 1000 ]
                        then
                                echo "${d}nginx重启后举行仍大于1000,请登录服务器处置" | mail -s "nginx 历程监控" 383xxxxx@qq.com
                                exit
                        fi
                fi
        fi
        sleep 20
done