当前位置: 移动技术网 > 网络运营>服务器>Linux > 监控php-fpm并自动重启服务的shell脚本

监控php-fpm并自动重启服务的shell脚本

2017年12月12日  | 移动技术网网络运营  | 我要评论

脚本代码:

复制代码 代码如下:

#!/bin/bash

#变量初始化
process="php-fpm" #进程名
startcmd="/etc/init.d/php-fpm start" #启动命令
down=0

while true
do
    #取得http状态码
    code=$(curl -h "host:www.jb51.net" -m 5 -l -s -w %{http_code} http://127.0.0.1 -o /dev/null)
    #当状态码返回000或者大于等于500时,计数故障到down变量
    if [ $code -eq 000 -o $code -ge 500 ];then
        ((down++))
    else
break
    fi
    #稍等5s
    sleep 5
    #判断是否连续检测三次都为故障.
    if [ $down -ge 3 ];then
if [ "$(find /tmp/${process}_restart -mmin -3)" == "" ];then
                #取得进程名对应的所有pid
pids=$(ps aux | grep ${process} | grep -v "grep" | awk '{print $2}')
                #依次对所有pid执行kill命令
for i in $pids;do
kill -9 $i
kill -9 $i
done
                #kill完pid后,启动服务
$startcmd
echo "$(date) return code $code,${process} had been restarted" >> /tmp/${process}_restart
else
echo "$(date) ${process} not yet recovery.as it had been restarted in 2 minutes.so this time ignore." >> /tmp/${process}_not_restart
fi

break
    fi
done

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网