PHP脚本监控Nginx 502错误并自动重启php-fpm,nginxphp-fpm_PHP教程

WBOY
Release: 2016-07-13 09:54:02
Original
1200 people have browsed it

PHP脚本监控Nginx 502错误并自动重启php-fpm,nginxphp-fpm

最近服务器时不时出现Nginx 502 Bad Gateway,如果在电脑旁边还好,要是半夜或者出去了,怎么办?
没关系,写个脚本检测服务状态,发现异常,自动重启。
自动重启脚本:
复制代码 代码如下:
$url = 'http://blog.rebill.info';
$cmd = '/usr/local/php/sbin/php-fpm restart';
 
for($i = 0; $i         $exec = "curl  connect-timeout 3 -I $url 2>/dev/null";
        $res = shell_exec($exec);
 
        if(stripos($res, '502 Bad Gateway') !== false){
                shell_exec($cmd);
                exit();
        }
}

原理就是用curl获取HTTP头,发现502状态码就执行重启php-fpm的命令。
url和cmd根据实际情况改成自己的。然后放到crontab里一分钟执行一次。
复制代码 代码如下:
*/1 * * * * /usr/bin/php /root/crontab/nginx502.php

大功告成!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/998803.htmlTechArticlePHP脚本监控Nginx 502错误并自动重启php-fpm,nginxphp-fpm 最近服务器时不时出现Nginx 502 Bad Gateway,如果在电脑旁边还好,要是半夜或者出去了,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!