#!/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