转贴502自动重启php-fpm

我的502 Bad Gateway问题主要是php-fpm导致的,所以为了更省心的去检查vps的状态我基本上使用定时脚本来维护vps的运行,下面转载一个502脚本

vps版本centos5.6

nginx 1.0.3

php 5.3.5/5.3.6

建立脚本  vim 502.sh

   

#!/bin/bash
##
#Program:502
##
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# web_URL #
site=http://www.baidu.com
# mailto #
receiver_email="接收邮箱@139.com"
sender_email="发送邮箱@gmail.com"
recevier="接收名"
sender="发送名"
# email #
function run_502(){
    sendmail -t <<EOF
From: $sender <$sender_email>
To: $receiver <$receiver_email>
Subject: VPS has encountered a 502 error!
-------------------------------------------------------------------
The VPS has encountered a 502 error, and it has been handled.
Congratulations! The VPS runs well now!
-------------------------------------------------------------------
EOF
}
function restart_error(){
    sendmail -t <<EOF
From: $sender <$sender_email>
To: $receiver <$receiver_email>
Subject: A terrible error occurred to the VPS.
------------------------------------------------------------------
The VPS has encountered an error, but it failed to restart php-fpm.
The information has already saved to /home/wwwlogs/502log.log.
------------------------------------------------------------------
EOF
}
# test #
stat=`curl -I $site | head -1 | cut -d ' ' -f2`
if [ $stat == "502" ]; then
    service php-fpm restart 2>>/home/wwwlogs/502log.log
    temp=`echo $?`
    if [ $temp != "0" ]; then
        restart_error
    else
        run_502
    fi
fi
exit 0

脚本出自: www.ninthday.net/2011/05/auto-restart-httpd-when-error.html

保存并授权chmod 755 502.sh

添加定时任务   我的脚本路径/home/502.sh 执行crontab –e

内容:

*/10 * * * * (/home/502.sh >> /home/wwwlogs/502log.log)

解释:每十分钟检查一次状态并写记录。crontab –l检查任务添加。

开始测试效果:

先service php-fpm stop然后top -c看看是否停掉了php-fpm

然后执行/home/502.sh后再次top -c应该重启了php-fpm和发送了邮件到你指定的邮箱了。

去打赏

您的支持将鼓励我们继续创作!

[微信] 扫描二维码打赏

[支付宝] 扫描二维码打赏

正在跳转到PayPal...

未经允许不得转载:飞翔线 » 转贴502自动重启php-fpm