监控php-fpm并自动重启服务的shell脚本
脚本代码:
#!/bin/bash
#变量初始化
process="php-fpm"#进程名
startCmd="/etc/init.d/php-fpmstart"#启动命令
down=0
whiletrue
do
#取得http状态码
code=$(curl-H"Host:www.nhooo.com"-m5-L-s-w%{http_code}http://127.0.0.1-o/dev/null)
#当状态码返回000或者大于等于500时,计数故障到down变量
if[$code-eq000-o$code-ge500];then
((down++))
else
break
fi
#稍等5s
sleep5
#判断是否连续检测三次都为故障.
if[$down-ge3];then
if["$(find/tmp/${process}_restart-mmin-3)"==""];then
#取得进程名对应的所有pid
pids=$(psaux|grep${process}|grep-v"grep"|awk'{print$2}')
#依次对所有pid执行kill命令
foriin$pids;do
kill-9$i
kill-9$i
done
#kill完pid后,启动服务
$startCmd
echo"$(date)Returncode$code,${process}hadbeenrestarted">>/tmp/${process}_restart
else
echo"$(date)${process}notyetrecovery.Asithadbeenrestartedin2minutes.sothistimeignore.">>/tmp/${process}_not_restart
fi
break
fi
done