放到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 ]