linux apache 自动监护脚本

 1 首先安装curl

yum install curl

2 编写shell

vi restart_apache.sh

写入一下内容

#!/bin/bash
URL="http://127.0.0.1/"
curlit()
{
curl --connect-timeout 35 --max-time 40 --head --silent "$URL" | grep '200'
}

doit()
{
if ! curlit; then
sleep 20
top -n 1 -b >> /var/log/restart_log/apache.log
/etc/init.d/httpd stop
sleep 2
/etc/init.d/httpd start > /dev/null
echo $(date) "Apache Restart" >> /var/log/restart_log/re_apache.log
sleep 30
if ! curlit; then
echo $(date) "Failed! Now Reboot Computer!" >> /var/log/restart_log/rebot_apache.log
reboot
fi
sleep 180
fi
}

sleep 300
while true; do
doit > /dev/null
sleep 10
done

3 把restart_apache.sh 加入到定时计划任务

crontab -e

写入以下内容 

  */1 * * * * /home/restart_apache.sh  //一分钟执行一次

crontab -l 查看 是否加入成功

4 chmod +x  restart_apache.sh 表示可以执行

/sbin/service crond stop

5 测试 把apache停止  然后测试 过一会看看apache是否已经自动启动

http://www.cnitblog.com/201/archive/2010/06/17/66805.html

http://blog.csdn.net/xifeijian/article/details/8703903

http://www.bootf.com/599.html

原文地址:https://www.cnblogs.com/jackspider/p/3857382.html