linux apache 自动监护脚本

本文提供了一套完整的步骤指南,用于在Linux环境下自动化管理Apache服务的重启,包括安装curl、编写shell脚本、加入定时任务、设置执行权限及验证自动重启功能。通过实践案例,读者可以轻松实现Apache服务的自动管理和维护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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.youkuaiyun.com/xifeijian/article/details/8703903

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

转载于:https://www.cnblogs.com/jackspider/p/3857382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值