最近服务器时不时出现Nginx 502 Bad Gateway,如果在电脑旁边还好,要是半夜或者出去了,怎么办? 没关系,写个脚本检测服务状态,发现异常,自动重启。 自动重启脚本:
<?php 
$url = 'http://blog.rebill.info'; 
$cmd = '/usr/local/php/sbin/php-fpm restart'; 
  
for($i = 0; $i < 5; $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(); 
        } 
}
*/1 * * * * /usr/bin/php /root/crontab/nginx502.php