php-fpm监控与自动重启脚本实现
监控脚本逻辑: 复制代码 代码如下: #!/bin/bash #参数设定 process="php-fpm" #目标进程 startCmd="/etc/init.d/php-fpm start" #启动指令 failCount=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,则记录故障次数 if [ $code -eq 000 -o $code -ge 500 ];then ((failCount++)) else break fi #等待5秒继续检测 sleep 5 #若连续三次检测均为故障 if [ $failCount -ge 3 ];then if [ "$(find /tmp/${process}_restart -mmin -3)" == "" ];then #获取该进程所有PID pids=$(ps aux | grep ${process} | grep -v "grep" | awk '{print $2}') #遍历并强制终止这些进程 for i in $pids;do kill -9 $i kill -9 $i done #重启服务进程 $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