zoukankan      html  css  js  c++  java
  • Nginx 遇到 502 Bad Gateway 自动重启脚本

    放到crontab里一分钟执行一次。url和cmd根据自己的改。
    原理就是用curl获取HTTP头,发现502状态码就执行重启php-fpm的命令。

    <span style="color: #666666; font-style: italic;">#!/usr/bin/php
    </span><span style="color: #339933;"><!--</span><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.deepvps.com'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cmd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/usr/local/php/sbin/php-fpm restart'</span><span style="color: #339933;">;</span>
     
    <span style="color: #b1b100;">for</span><span style="color: #009900;">(</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;"><</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">++</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>         <span style="color: #000088;">$exec</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">"curl  connect-timeout 3 -I <span style="color: #006699; font-weight: bold;">$url</span> 2>/dev/null"</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">shell_exec</span><span style="color: #009900;">(</span><span style="color: #000088;">$exec</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
     
            <span style="color: #b1b100;">if</span><span style="color: #009900;">(</span><span style="color: #990000;">stripos</span><span style="color: #009900;">(</span><span style="color: #000088;">$res</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'502 Bad Gateway'</span><span style="color: #009900;">)</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
                    <span style="color: #990000;">shell_exec</span><span style="color: #009900;">(</span><span style="color: #000088;">$cmd</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
                    <span style="color: #990000;">exit</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">}</span>
    <span style="color: #009900;">}</span>
    <span style="color: #339933;">--></span>

    至于crontab,请自行Google搜索

    转自:http://www.hostloc.com/viewthread.php?tid=19208&page=1#pid235729

    原创文章如转载,请注明:转载自深度VPS [ http://www.deepvps.com ] 
  • 相关阅读:
    python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory
    php回溯
    PHPredis长连接pconnect
    php操作redis出现不报错就退出
    MSMQ消息队列
    消息队列使用的四种场景介绍(准载)
    Spring MVC参数封装传递
    Spring MVC配置实例
    .NET项目中使用PostSharp
    C#进阶系列——AOP
  • 原文地址:https://www.cnblogs.com/lexus/p/1826554.html
Copyright © 2011-2022 走看看