- 论坛徽章:
- 0
|
服务器时不时出现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();
- }
- }
复制代码 原理就是用curl获取HTTP头,发现502状态码就执行重启php-fpm的命令。
url和cmd根据实际情况改成自己的。然后放到crontab里一分钟执行一次。- */1 * * * * /usr/bin/php /root/crontab/nginx502.php
复制代码 |
|