服务器上跑的是LNMP环境,近期总是有502现象。502为网站访问的状态码,200正常,502错误是nginx最为普通的错误状态码。由于502只是暂时的,并且只要一重启php-fpm服务则502消失,但不重启的话,则会一直持续很长时间。所以有必要写一个监控脚本,监控访问日志的状态码,一旦发生502,则自动重启一下php-fpm。
我们设定:
1. access_log /data1/log/access.log
2. 脚本死循环,每10s检测一次
3. 重启php-fpm的方法是 /etc/init.d/php-fpm restart
参考脚本:
{{{密码回复可见}}}
我们设定:
1. access_log /data1/log/access.log
2. 脚本死循环,每10s检测一次
3. 重启php-fpm的方法是 /etc/init.d/php-fpm restart
参考脚本:
{{{密码回复可见}}}
0
#! /bin/bash
a=grep -q '502' /data1/log/access.log
while :; do;[ $a eq 502 ];do service /etc/init.d/php-fpm restart;sleep 10;done
a=grep -q '502' /data1/log/access.log
while :; do;[ $a eq 502 ];do service /etc/init.d/php-fpm restart;sleep 10;done
0
[root@OceanV sh]# cat check_php_fpm.sh
#!/bin/bash
#
url="http://192.168.9.9/cacti/index.php"
PROG=/etc/init.d/php-fpm
LOG=/tmp/php-cgi.log
#LOG=/data1/log/access_log
while true
do
time=`date`
status=$(curl -s --head "$url" | awk '/HTTP/ {print $2}')
#status=`awk -F\" '{print $3}' $LOG |cut -c -4`
if [ "$status" = "502" ]; then
echo "Restart php-cgi at ${time} " >> $LOG
$PROG restart
fi
sleep 10
done
#!/bin/bash
#
url="http://192.168.9.9/cacti/index.php"
PROG=/etc/init.d/php-fpm
LOG=/tmp/php-cgi.log
#LOG=/data1/log/access_log
while true
do
time=`date`
status=$(curl -s --head "$url" | awk '/HTTP/ {print $2}')
#status=`awk -F\" '{print $3}' $LOG |cut -c -4`
if [ "$status" = "502" ]; then
echo "Restart php-cgi at ${time} " >> $LOG
$PROG restart
fi
sleep 10
done
0
#!/bin/bash
data=
while :
dt=`date -d '-1 min' +%H%M`
do
data=`grep '$dt' /data1/log/access.log | grep '502' | wc -c`
if [ ! -z $data ];then
/etc/init.d/php-fpm restart
data=
fi
sleep 10
done
data=
while :
dt=`date -d '-1 min' +%H%M`
do
data=`grep '$dt' /data1/log/access.log | grep '502' | wc -c`
if [ ! -z $data ];then
/etc/init.d/php-fpm restart
data=
fi
sleep 10
done
0
- #!/bin/bash
- # Monitor Nginx access status { 502}.
- # Writen by Wangxiaoqiang 2014.11.27.
- while true
- do
- grep '502' /data1/log/access.log > /dev/null
- if [ $? -eq 0 ]
- then
- /etc/init.d/php-fpm restart
- fi
- sleep 10
- done
0
本帖最后由 王肖强 于 2014-11-27 15:45 编辑
- #!/bin/bash
- # Monitor Nginx access status { 502}.
- # Writen by Wangxiaoqiang 2014.11.27.
- while true
- do
- grep '502' /data1/log/access.log > /dev/null
- if [ $? -eq 0 ]
- then
- /etc/init.d/php-fpm restart > /dev/null
- fi
- sleep 10
- done
0
本帖最后由 wuhen 于 2015-2-25 14:59 编辑
第一个加入到计划周期任务中每10秒执行,第二个自己会每10秒执行
- #!/bin/bash
- log=/data1/log/access.log
- while :;
- do
- grep -q '502' $log
- if [ $? -eq 0 ]
- then
- /etc/init.d/php-fpm restart >/dev/null
- else
- exit 0
- fi
- done
- #!/bin/bash
- log=/data1/log/access.log
- while :;
- do
- grep -q '502' $log
- if [ $? -eq 0 ]
- then
- /etc/init.d/php-fpm restart >/dev/null
- fi
- sleep 10
- done
0
有点缺憾
#!/bin/bish
while : ;
do
awk '{ print $6 }' /tmp/access.log |cut -b -3 |grep 502
if [ $? -eq 0 ]
then
touch 1.txt >/dev/dull
else
/etc/init.d/php-fpm restart > echo reboot.txt
sed -i “s/502/555/g” /tmp/access.log
fi
sleep 10
done
#!/bin/bish
while : ;
do
awk '{ print $6 }' /tmp/access.log |cut -b -3 |grep 502
if [ $? -eq 0 ]
then
touch 1.txt >/dev/dull
else
/etc/init.d/php-fpm restart > echo reboot.txt
sed -i “s/502/555/g” /tmp/access.log
fi
sleep 10
done
0
#!/bin/bash
while :;do
if cat /data1/log/access.log |grep -q 502 ; then
/etc/init.d/php-fpm restart &>/dev/null
fi
sleep 10
done
while :;do
if cat /data1/log/access.log |grep -q 502 ; then
/etc/init.d/php-fpm restart &>/dev/null
fi
sleep 10
done
0
本帖最后由 sss 于 2015-6-1 14:44 编辑
- #!/bin/bash
- #Nginx Log inspection
- #502 error More than 5 to restart php-fpm
- log=/data1/log/access.log
- n=5
- while :
- do
- tail -n 100 $log > /tmp/log
- N_502=`grep -c '502' /tmp/log`
- if [ $N_502 -ge $n ]
- then
- /etc/init.d/php-fpm restart >/dev/null 2>&1
- sleep 10
- fi
- sleep 10
- done
0
a=`grep '200' /data1/log/access.log |wc -l` while : ; b=`grep '200' /data1/log/access.log |wc -l` if [ $b -lt $a ];then a=$b /etc/init.d/php-fpm restart fi sleep 10000 done
0
while : do if [ grep -p '502' /data1/log/access.log ] /etc/init.d/php-fpm restart > /dev/null 2>/dev/null n=ps-ef|grep -p -v 'php-fpm if [ $n -eq 0 ] /etc/init.d/php-fpm restart > /dev/null 2>/data1/log/php_start.err else mail -s "php-fpm restart error" 1357970430@qq.com < /data1/log/php_start.err fi fi sleep 10 done
0
- #!/bin/bash
- while :
- do
- tail -n1 /home/wwwlogs/bbs.maria.com-access_log >./log
- ztm=` awk '{print $9}' ./log`
- if [ $ztm -eq '502' ]
- then
- /etc/init.d/php-fpm restart
- fi
- sleep 10
- done
0
#!/bin/bash
while :
do
err=`cat /usr/local/apache2/logs/access_log |awk {'print $9'} |grep 502|wc -l`
if [ $err != 0 ]
then
/etc/init.d/php-fpm restart
else
exit 0
fi
sleep 10
done
while :
do
err=`cat /usr/local/apache2/logs/access_log |awk {'print $9'} |grep 502|wc -l`
if [ $err != 0 ]
then
/etc/init.d/php-fpm restart
else
exit 0
fi
sleep 10
done
0
#!/bin/bash
while [ 1 ]
do
a=`grep -q '502' /data1/log/access.log`
if [ $a -eq 502 ]
then
/etc/init.d/php-fpm restart
fi
>/data1/log/access.log
sleep 10
done
while [ 1 ]
do
a=`grep -q '502' /data1/log/access.log`
if [ $a -eq 502 ]
then
/etc/init.d/php-fpm restart
fi
>/data1/log/access.log
sleep 10
done
0
#!/bin/bash
while :
do
if head /data1/log/access.log|grep '501';then
/etc/init.d/php-fpm restart
fi
sleep 10
done
while :
do
if head /data1/log/access.log|grep '501';then
/etc/init.d/php-fpm restart
fi
sleep 10
done
0
/bin/bash
if grep -q "502" /data1/log/access.log
then
>/data1/log/access.log
/etc/init.d/php-fpm restart
fi
while :
do
sleep 10
/bin/bash /usr/local/sbin/0519/check502.sh
done
if grep -q "502" /data1/log/access.log
then
>/data1/log/access.log
/etc/init.d/php-fpm restart
fi
while :
do
sleep 10
/bin/bash /usr/local/sbin/0519/check502.sh
done
0
#! /bin/bash
while [ -n `cat /data1/log/access.log |grep '502'` ]
do
/etc/init.d/php-fpm restart
sleep 10
done
while [ -n `cat /data1/log/access.log |grep '502'` ]
do
/etc/init.d/php-fpm restart
sleep 10
done
编辑回复