脚本内容:
#!/bin/bash
funCheck()
{
if [ -z "`ps aux |grep php-fpm |grep -v grep`" ];then
if [ -z "`cat /usr/local/php/php-fpm.pid `" ];then
echo "php-fpm process doesn't exist ,you can start it now `date`" >> $Log
else
echo "php-fpm process exists still `date`" >> $Log
fi
fi
}
Log=/home/sysadmin/zhaoyj/log/PhpFpm.log
ChildProcess=`ps aux |grep php-fpm |grep -v grep |grep "^www"|wc -l`
DATE=`date "+%Y%m%d-%H:%M:%S"`
Message="MG Server php-fpm child process number is $ChildProcess at $DATE"
Admin="邮件地址"
if [ -e /var/lock/subsys/502 ]
then
if [ $ChildProcess -ge 100 ];then
echo "$Message" |mail -s "$Message" $Admin
echo "$Message" >> $Log
fi
Record=``ps aux |grep php-fpm|grep -v grep |awk '{print $3"\t"$2"\t"$11}'|tr -d :|sort -nr|head -1`
CPU=`awk '{print $1}' $Record`
PID=`awk '{print $2}' $Record`
Result=`awk -v NUM1=$CPU -v NUM2=50 'BEGIN{print(NUM1>NUM2)?"1":"0"}'`
Message2="php-fpm child process CPU% is $Result"
if [ $Result -eq 0 ];then
echo "$Message2 ,normal now $DATE" >> $Log
else
echo "$Message2 ,not normal now $DATE" >> $Log
echo "$Message2 ,not normal now $DATE" |mail -s "$Message2 ,not normal now $DATE" $Admin
fi
killall -9 curl 2>/dev/null
killall -9 php-fpm 2>/dev/null
funCheck
service php-fpm start >/dev/null
echo "[ `date +'%h %d %T'` ] PHP-FPM died with no response, all processes restarted">>/usr/local/php/log/php-fpm-restart.log
else
touch /var/lock/subsys/502
if [ `curl --connect-timeout 5 -IURL 2>/dev/null | grep '502 Bad Gateway' -c` != '0' ]
then
killall -9 php-fpm 2>/dev/null
funCheck
service php-fpm start >/dev/null
echo "[ `date +'%h %d %T'` ] PHP-FPM died with 502 bad gateway, all processes restarted">>/usr/local/php/log/php-fpm-restart.log
fi
rm -f /var/lock/subsys/502
fi