監(jiān)控php-fpm并自動重啟服務(wù)的shell腳本
更新時間:2014年05月27日 10:08:27 作者:
這篇文章主要介紹了監(jiān)控php-fpm并自動重啟服務(wù)的shell腳本,腳本的主要功能:不斷檢查網(wǎng)站的狀態(tài),如果異常就重啟php-fpm服務(wù),需要的朋友可以參考下
腳本代碼:
復(fù)制代碼 代碼如下:
#!/bin/bash
#變量初始化
process="php-fpm" #進程名
startCmd="/etc/init.d/php-fpm start" #啟動命令
down=0
while true
do
#取得http狀態(tài)碼
code=$(curl -H "Host:www.dbjr.com.cn" -m 5 -L -s -w %{http_code} http://127.0.0.1 -o /dev/null)
#當狀態(tài)碼返回000或者大于等于500時,計數(shù)故障到down變量
if [ $code -eq 000 -o $code -ge 500 ];then
((down++))
else
break
fi
#稍等5s
sleep 5
#判斷是否連續(xù)檢測三次都為故障.
if [ $down -ge 3 ];then
if [ "$(find /tmp/${process}_restart -mmin -3)" == "" ];then
#取得進程名對應(yīng)的所有pid
pids=$(ps aux | grep ${process} | grep -v "grep" | awk '{print $2}')
#依次對所有pid執(zhí)行kill命令
for i in $pids;do
kill -9 $i
kill -9 $i
done
#kill完pid后,啟動服務(wù)
$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
相關(guān)文章
Shell中實現(xiàn)字符串反轉(zhuǎn)方法分享
這篇文章主要介紹了Shell中實現(xiàn)字符串反轉(zhuǎn)方法分享,本文同時提供了多種語言的實現(xiàn)方法,如awk、python、bash、C語言等,需要的朋友可以參考下2014-12-12Shell特殊變量(Shell $#、$*、$@、$?、$$)的使用
這篇文章主要介紹了Shell特殊變量(Shell $#、$*、$@、$?、$$)的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Linux下使用Shell腳本實現(xiàn)ftp的自動上傳下載的代碼小結(jié)
如何實現(xiàn)在ftp自動上傳下載文件腳本呢?想必還有很多朋友都不太了解吧,下面是ftp自動上傳下載文件腳本方法,希望對朋友們有些幫助2013-02-02