腳本代碼:
 代碼如下:
#!/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
新聞熱點
疑難解答