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