Home > Backend Development > PHP Tutorial > PHP脚本监控Nginx 502错误并自动重启php-fpm_PHP

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

WBOY
Release: 2016-06-01 14:35:22
Original
1522 people have browsed it

最近服务器时不时出现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


大功告成!
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