php-fpm死機解決辦法,指令碼後台自動重啟

來源:互聯網
上載者:User

標籤:txt   tar   err   shell   count   sleep   停止   dir   ebs   

本人用nginx+php7搭建了一台伺服器,因為請求量太大,而且php裡面又有掛起的任務,導致php-fpm在高峰期的時候經常死掉,吧php-fpm的最大進程數已經改到1000了,還是吃不消,cpu也是超負荷,每次都要手動重啟,太煩人,因此本人寫了一個shell指令碼,後台監聽php-fpm,等達到一定數量讓他重啟,這樣就有效解決了手動重啟,死機問題了。

1.聲明文檔頭跟定義變數

#!/usr/bin/env bash

maxCount=300 #php-fpm最大進程數

basePath=$(cd `dirname $0`; pwd) #指令碼所在目錄

pidFilePath="$basePath/pid.conf" #pid存放檔案

errorFilePath="$basePath/error.txt" #錯誤記錄檔存放檔案

2.定義關鍵函數

getDateTime(){

    date "+%Y-%m-%d %H:%M:%S"

}

restart(){

    kill-USR2$(cat /usr/local/php/var/run/php-fpm.pid)

}

start(){

    /usr/local/php/sbin/php-fpm

}

stop(){

    kill $(cat /usr/local/php/var/run/php-fpm.pid)

}

stopweb(){

if [ -f "$pidFilePath" ];then

    kill $(cat "$pidFilePath")2>/dev/null

    rm -r "$pidFilePath"

fi

}

main(){

stopweb

echo "$$">>"$pidFilePath"

while((1))

do

    count=$(ps aux | grep -c php-fpm)

    if (("$count">="$maxCount"));then

        restart

        date=$(getDateTime)

        echo "date:$date,maxCount:$maxCount,count:$count">>"$errorFilePath"

    fi

    sleep 10

done

}

3.加入啟動參數

case $1 in

    "restart") restart

    ;;

    "start") start

    ;;

    "stop") stop

    ;;

    "run") main

    ;;

    "stopweb") stopweb

    ;;

    *) echo "cmd error!"

    ;;

esac

 

4.終止指令碼

exit 0

運行命令

./webserver restart #重啟php-fpm

./webserver start #開啟php-fpm

./webserver stop #停止php-fpm

setsid ./webserver run & #開啟監聽,後台運行&

./webserver stopweb #停止監聽

完整指令碼下載在qq群裡面:群號490328630

php-fpm死機解決辦法,指令碼後台自動重啟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.