PHP-FPM dead solution, script background auto restart

Source: Internet
Author: User

I used NGINX+PHP7 to build a server, because the request volume is too large, and PHP inside there are suspended tasks, resulting in php-fpm in the peak time often die, bar php-fpm The maximum process has been changed to 1000, or too much, the CPU is overloaded, Each time to manually restart, too annoying, so I wrote a shell script, background monitoring php-fpm, and so on to a certain number to let him restart, so that effectively solve the manual restart, the crash problem.

1. Declaring document headers and defining variables

#!/usr/bin/env Bash

maxcount=300 #php-fpm Maximum number of processes

basepath=$ (CD ' DirName $ '; pwd) #脚本所在目录

Pidfilepath= "$basePath/pid.conf" #pid存放文件

Errorfilepath= "$basePath/error.txt" #错误日志存放文件

2. Defining key functions

GetDateTime () {

Date "+%y-%m-%d%h:%m:%s"


Restart () {

kill-usr2$ (Cat/usr/local/php/var/run/


Start () {



Stop () {

Kill $ (Cat/usr/local/php/var/run/


Stopweb () {

If [-F "$pidFilePath"];then

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

Rm-r "$pidFilePath"



Main () {


echo "$$" >> "$pidFilePath"

while ((1))


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

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


date=$ (GetDateTime)

echo "Date: $date, MaxCount: $maxCount, Count: $count" >> "$errorFilePath"


Sleep 10



3. Join the Startup parameters

Case $ in

"Restart") restart


"Start") Start


"Stop") stop


"Run") main


"Stopweb") stopweb


*) echo "cmd error!"



4. Terminating the script

Exit 0

Run command

./webserver Restart #重启php-fpm

./webserver Start #开启php-fpm

./webserver Stop #停止php-FPM

Setsid./webserver Run & #开启监听, running in the background &

./webserver Stopweb #停止监听

Full script download in QQ Group: Group No. 490328630

PHP-FPM dead solution, script background auto restart

Related Article

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: 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.