admin 发表于 2023-3-2 19:25:49

Nginx 502 Bad Gateway 自动重启脚本

<p>需要确保系统已安装curl,centos可以执行:yum install curl,debian/ubuntu可以执行:apt-get install curl</p>
<p>用WinSCP或vi有或nano在/root目录下创建502.sh 内容如下:</p>
<pre>#!/bin/bash
# author: licess
# website: http://www.bfht.net

CheckURL="http://www.wangchao.info"

STATUS_CODE=`curl -o /dev/null -m 10 --connect-timeout 10 -s -w %{http_code} $CheckURL`
#echo "$CheckURL Status Code:\t$STATUS_CODE"
if [ "$STATUS_CODE" = "502" ]; then
      /etc/init.d/php-fpm restart
fi
</pre>
<p>chmod +x /root/502.sh<br>
用crontab 一分钟执行一次,上面的http://www.wangchao.info改成你的地址,如果该页面是静态,需换成以php的页面地址。</p>
<p><span style="font-size: 14pt;"><strong>Crontab的使用:</strong></span></p>
<p><strong>CentOS下面安装Crontab</strong></p>
<pre>yum install vixie-cron crontabs      //安装Crontab

chkconfig crond on                //设为开机自启动
service crond start               //启动
</pre>
<p>说明:vixie-cron软件包是cron的主程序;crontabs软件包是用来安装、卸装、 或列举用来驱动 cron 守护进程的表格的程序。</p>
<p><strong>Debian下面安装Crontab</strong></p>
<pre>apt-get install cron             //大部分情况下Debian都已安装。

/etc/init.d/cron restart    //重启Crontab</pre>
<p><strong>查看crontab定时执行任务列表</strong></p>
<pre>crontab -l</pre>
<p><strong>添加crontab定时执行任务</strong></p>
<pre>crontab -e</pre>
<p>输入crontab任务命令时可能会因为crontab默认编辑器的不同。</p>
<p><strong>crontab 任务命令书写格式</strong></p>
<table width="60%">
<tbody>
<tr>
<td>格式:</td>
<td>minute</td>
<td>hour</td>
<td>dayofmonth</td>
<td>month</td>
<td width="11%">dayofweek</td>
<td width="21%">command</td>
</tr>
<tr>
<td>解释:</td>
<td>分钟</td>
<td>小时</td>
<td>日期</td>
<td>月付</td>
<td>周</td>
<td>命令</td>
</tr>
<tr>
<td>范围:</td>
<td>0-59</td>
<td>0~23</td>
<td>1~31</td>
<td>1~12</td>
<td>0~7,0和7都代表周日</td>
</tr>
</tbody>
</table>
<p>在crontab中我们会经常用到* ,   –  /n 这4个符号,好吧还是再画个表格,更清楚些:</p>
<table border="1" width="60%">
<tbody>
<tr>
<td>符号</td>
<td>解释</td>
</tr>
<tr>
<td>*(星号)</td>
<td>代表所有有效的值。 如:0 23 * * * backup 不论几月几日周几的23点整都执行backup命令。</td>
</tr>
<tr>
<td>,(逗号)</td>
<td>代表分割开多个值。如:30 9 1,16,20 * * command 每月的1、16、20号9点30分执行command命令。</td>
</tr>
<tr>
<td>-(减号)</td>
<td>代表一段时间范围。如0 9-17 * * * checkmail 每天9点到17点的整点执行checkmail命令</td>
</tr>
<tr>
<td>/n</td>
<td>代表每隔n长时间。如*/5 * * * * check 每隔5分钟执行一次check命令,与0-59/5一样。</td>
</tr>
</tbody>
</table>
<p><strong>下面举一些例子来加深理解:</strong></p>
<p>每天凌晨3:00执行备份程序:0 3 * * * /root/backup.sh</p>
<p>每周日8点30分执行日志清理程序:30 8 * * 7 /root/clear.sh</p>
<p>每周1周5 0点整执行test程序:0 0 * * 1,5 test</p>
<p>每年的5月12日14点执行wenchuan程序:0 14 12 5 * /root/wenchuan</p>
<p>每晚18点到23点每15分钟重启一次php-fpm:*/15 18-23 * * * /etc/init.d/php-fpm</p>
                                        <p class="post-copyright">未经允许不得转载:<ahref="https://www.wangchao.info/">王超博客</a> &raquo; <ahref="https://www.wangchao.info/730.html">Nginx 502 Bad Gateway 自动重启脚本</a></p>
页: [1]
查看完整版本: Nginx 502 Bad Gateway 自动重启脚本