日期:2014-05-16  浏览次数:21028 次

apache服务监控脚本-----自动重启apache
[root@localhost shell]# vi apache_monitor.sh 
#!/bin/bash
while [ 1 ]
do
        wget http://127.0.0.1/a.html > /dev/null 2>&1
        if [ $? -ne 0 ]
        then
                service apache restart >/dev/null 2>&1
        else
                wt=`cat a.html`
                if [ $wt != 'OK' ]
                then
                        service apache restart > /dev/null 2>&1
                fi
        fi
sleep 2
done


[root@localhost shell]# cat /www/a.html 

OK

[root@localhost ~]# lsof -i:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE NODE NAME
httpd   32105   root    3u  IPv4 409340       TCP *:http (LISTEN)
httpd   32109 daemon    3u  IPv4 409340       TCP *:http (LISTEN)
httpd   32111 daemon    3u  IPv4 409340       TCP *:http (LISTEN)
httpd   32113 daemon    3u  IPv4 409340       TCP *:http (LISTEN)
[root@localhost ~]# killall httpd
[root@localhost ~]# lsof -i:80
[root@localhost ~]# lsof -i:80   在我杀死httpd进程的大概一两秒时间内,apache服务没有起来,但是很快就可以了,

如果是在实际生产中,可以把脚本中的sleep时间改成1.
COMMAND PID USER   FD   TYPE DEVICE SIZE NODE NAME
httpd   618 root    3u  IPv4 411612       TCP *:http (LISTEN)
[root@localhost ~]# lsof -i:80
COMMAND PID   USER   FD   TYPE DEVICE SIZE NODE NAME
httpd   618   root    3u  IPv4 411612       TCP *:http (LISTEN)
httpd   623 daemon    3u  IPv4 411612       TCP *:http (LISTEN)
httpd   625 daemon    3u  IPv4 411612       TCP *:http (LISTEN)
httpd   627 daemon    3u  IPv4 411612       TCP *:http (LISTEN)

[root@localhost ~]# cd /shell/
[root@localhost shell]# ls
access.log  a.html.176  a.html.254  a.html.332  a.html.410
a.html      a.html.177  a.html.255  a.html.333  a.html.411
a.html.1    a.html.178  a.html.256  a.html.334  a.html.412
a.html.10   a.html.179  a.html.257  a.html.335  a.html.413
a.html.100  a.html.18   a.html.258  a.html.336  a.html.414
a.html.101  a.html.180  a.html.259  a.html.337  a.html.415
a.html.102  a.html.181  a.html.26   a.html.338  a.html.416
a.html.103  a.html.182  a.html.260  a.html.339  a.html.417
a.html.104  a.html.183  a.html.261  a.html.34   a.html.418
a.html.105  a.html.184  a.html.262  a.html.340  a.html.419

a.html.106  a.html.………………

我擦,发现爆表了,忘记了,脚本还没有完善,需要把这东西全干掉

而且我还想要记录apache的重启时间和次数,所以还需要更改脚本

#!/bin/bash
while [ 1 ]
do
        wget http://