Php-fpm dead solution, script background restarts automatically

Source: Internet
Author: User

Php-fpm dead solution, script background restarts automatically

I used nginx + php7 to build a server, because the request volume is too large, and there are pending tasks in php, causing the php-fpm to often die during peak hours, the maximum number of php-fpm processes has been changed to 1000, which is still too high. The cpu is also overloaded. It is annoying to manually restart each time. Therefore, I wrote a shell script, the backend listens to the php-fpm and allows it to restart after it reaches a certain number. This effectively solves the manual restart and crash issues.

1. Declare the document header and define variables

#! /Usr/bin/env bash

MaxCount = 300 # maximum number of php-fpm Processes

BasePath = $ (cd 'dirname $ 0'; pwd) # directory of the script

PidFilePath = "$ basePath/pid. conf" # saving files with pid

ErrorFilePath = "$ basePath/error.txt" # file storage for error logs

2. Define key functions

GetDateTime (){

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

}

Restart (){

The 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. Add startup parameters

Case $1 in

"Restart") restart

;;

"Start") start

;;

"Stop") stop

;;

"Run") main

;;

"Stopweb") stopweb

;;

*) Echo "cmd error! "

;;

Esac

4. Terminate the script

Exit 0

Run commands

./Webserver restart # restart php-fpm

./Webserver start # enable php-fpm

./Webserver stop # stop php-fpm

Setsid./webserver run & # enable listening and run in the Background &

./Webserver stopweb # Stop listening

Download the complete script to the QQ Group: Group number 490328630

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.