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/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. Join the Startup parameters
Case $ in
"Restart") restart
;;
"Start") Start
;;
"Stop") stop
;;
"Run") main
;;
"Stopweb") stopweb
;;
*) echo "cmd error!"
;;
Esac
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