很多时候网站服务器挂掉手工重启是件很麻烦的事情,使用以下方法可以做到自动重启,并且生成相应的日志记录。相关文章:Ubuntu 下监控并自动重启网卡
1 创建被监控的文件 OK.php
内容如下:
PHP
OK
1OK
为了尽可能简单,这里只用一个内容为 OK 的空 php 文件作为监控文件。
当然如果你想要监控其他状态不妨也写在这个 OK.php,例如监控 MySQL 连接是否在正常:
PHP
$servername = "localhost";
$username = "root";
$password = "*******";
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "OK";
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14<?php
$servername="localhost";
$username="root";
$password="*******";
// 创建连接
$conn=newmysqli($servername,$username,$password);
// 检测连接
if($conn->connect_error){
die("连接失败: ".$conn->connect_error);
}
echo"OK";
?>
总之,只要正常时返回 OK 即可。
上传此文件到服务器根目录:/var/www/
2 创建脚本 auto_restart_apache.sh
内容如下:
Shell
#!/bin/bash
wget http://127.0.0.1/OK.php > /dev/null 2>&1
if [ $? -ne 0 ]
then
#lnmp restart >/dev/null 2>&1
service apache2 restart >/dev/null 2>&1
service httpd restart >/dev/null 2>&1
echo "apache restarted at `date +%y-%m-%d\ %H:%M:%S`" >> /alidata/tools/apache_restart_logs.txt
else
wt=`cat OK.php `
echo $wt
if [ $wt != 'OK' ]
then
#lnmp restart > /dev/null 2>&1
service apache2 restart >/dev/null 2>&1
service httpd restart >/dev/null 2>&1
echo "apache restarted at `date +%y-%m-%d\ %H:%M:%S`" >> /alidata/tools/apache_restart_logs.txt
else
echo "apache works fine at `date +%y-%m-%d\ %H:%M:%S`" >> /alidata/tools/apache_restart_logs.txt
fi
fi
rm -rf OK.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#!/bin/bash
wgethttp://127.0.0.1/OK.php>/dev/null2>&1
if[$?-ne0]
then
# lnmp restart >/dev/null 2>&1
serviceapache2restart>/dev/null2>&1
servicehttpdrestart>/dev/null2>&1
echo"apache restarted at `date +%y-%m-%d\ %H:%M:%S`">>/alidata/tools/apache_restart_logs.txt
else
wt=`catOK.php`
echo$wt
if[$wt!='OK']
then
# lnmp restart > /dev/null 2>&1
serviceapache2restart>/dev/null2>&1
servicehttpdrestart>/dev/null2>&1
echo"apache restarted at `date +%y-%m-%d\ %H:%M:%S`">>/alidata/tools/apache_restart_logs.txt
else
echo"apache works fine at `date +%y-%m-%d\ %H:%M:%S`">>/alidata/tools/apache_restart_logs.txt
fi
fi
rm-rfOK.php
上传此文件到服务器。
注释:
1)该脚本会读取 OK.php 文件并且在本地生成 apache_restart_logs.txt 日志文件。
2)如果不能访问到 OK.php 则重启 apache 服务。
3 创建定时任务
创建定时任务有两种方法一种是直接编辑 /etc/crontab,运行:
PowerShell
sudo vim /etc/crontab
1sudovim/etc/crontab
在后面添加:
PowerShell
*/30 * * * * root /alidata/tools/auto_restart_apache.sh >> /alidata/tools/logs.txt 2>&1
1*/30****root/alidata/tools/auto_restart_apache.sh>>/alidata/tools/logs.txt2>&1
其中 */30 表示每 30 分钟执行一次脚本。
关于 crontab 时间说明如下:
PowerShell
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR
#sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
1
2
3
4
5
6
7
8# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR
#sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
另一种是使用命令:
PowerShell
crontab -e
1crontab-e
在后面添加:
PowerShell
*/30 * * * * /alidata/tools/auto_restart_apache.sh >> /alidata/tools/logs.txt 2>&1
1*/30****/alidata/tools/auto_restart_apache.sh>>/alidata/tools/logs.txt2>&1
我的理解,这两种方式前者添加的是一个整个系统所有用户的定时任务,所以在时间后面需要指定用户 root。后者是当前用户的定任务。采用哪一种都可以。
重启定时任务:
PowerShell
sudo service crond restart
1sudoservicecrondrestart
如果顺利的话这一脚本就会每隔 30 分钟执行一次,并且可以在 /alidata/tools/apache_restart_logs.txt 看到脚本执行的日志以及 /alidata/tools/logs.txt 看到每次执行的输出。因为频率低以及只输出不多的几个字符,这两个 log 文件通常不会增加很多,不过也应当注意下及时清除。
Original content here is published under these license terms:X
License Type:Read Only
License Abstract:You may read the original content in the context in which it is published (at this web address). No other copying or use is permitted without written agreement from the author.