长虫山小木屋

没有谁会为你踏雪而来 喜欢的风景要躬亲筚路

  博客园  :: 首页  :: 新随笔  :: 联系 ::  :: 管理
#!/bin/bash
###获取当前时间
time="$(date +"%Y%m%d-%H:%M")"
###查看fpm服务是否运行
i=`netstat -an | grep php-cgi | wc -l`
if [ $i = 0 ]
        then
               ###重启php服务
               /etc/init.d/php-fpm restart
               ### 写入日志
               echo "$time php-fpm service is down .... restart..." >> /home/checkfail-lnmp.log
fi
###查看mysql服务是否运行
i=`netstat -anpt | grep mysqld | awk '{print $4}' | awk -F: '{print $2}' | wc -l`
if [ $i = 0 ]
        then
               ### 重启mysql服务
               /etc/init.d/mysql restart
               ### 写入日志
               echo "$time mysqld service is down .... restart..." >> /home/checkfail-lnmp.log
fi
###查看nginx服务是否运行
i=`netstat -anpt | grep nginx | awk '{print $4}' | awk -F: '{print $2}' | wc -l`
if [ $i = 0 ]
        then
               ### 重启nginx服务
               /etc/init.d/nginx restart
               ### 写入日志
               echo "$time nginx service is down .... restart..." >>  /home/checkfail-lnmp.log
fi

如果要监控url是否是200:

## 判断状态码是否为200
url=https://yourdomain.com
i=$(curl -I -m 10 -o /dev/null -s -w %{http_code} $url)
if [ $i -ne 200 ]; then
                 /etc/init.d/mysql restart
                 /etc/init.d/nginx restart
                 /etc/init.d/php-fpm restart
                 echo " $time 监测页: $url 状态码: $i 行为: 异常&重启" >> /home/checkfail-lnmp.log
fi

脚本添加执行权限:chmod a+x /root/checklnmp.sh

crontab每分钟检查一次:

*/1 * * * * bash /root/checklnmp.sh

 

posted on 2024-03-20 21:41  长虫山小木屋  阅读(6)  评论(0编辑  收藏  举报