shell腳本怎樣實(shí)現(xiàn)定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“shell腳本怎樣實(shí)現(xiàn)定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“shell腳本怎樣實(shí)現(xiàn)定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)”這篇文章吧。

目前創(chuàng)新互聯(lián)公司已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、卡若網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

注意:監(jiān)控方法可以為端口、進(jìn)程、URL模擬訪(fǎng)問(wèn)方式,或者三種方法綜合。

說(shuō)明:由于截止到目前僅講了if語(yǔ)句,因此,就請(qǐng)大家用if語(yǔ)句來(lái)實(shí)現(xiàn)。

  [root@oldboy-B scripts]# cat apachemon
  #!/bin/sh
  #created by oldboy 20110523
  . /etc/init.d/functions
  HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
  #if [ $HTTPPRONUM -lt 1 ];then
  if [[ $HTTPPRONUM -lt 1 ]];then
  action “httpd is not running” /bin/false
  action “httpd is not running” /bin/false >/tmp/httpd.log
  httpdctl restart >/dev/null 2>&1
  action “httpd is restart” /bin/true
  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com
  exit 1
  else
  action “httpd is running” /bin/true
  exit 0
  fi
  [root@oldboy-B scripts]# apachemon
  httpd is running [確定]
  [root@oldboy-B scripts]# pkill httpd
  [root@oldboy-B scripts]# ps -ef |grep http |grep -v grep
  [root@oldboy-B scripts]# apachemon
  httpd is not running [失敗]
  httpd is restart [確定]
  [root@oldboy-B scripts]# ps -ef|grep http|grep -v grep
  root 5845 1 1 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5852 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5853 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5854 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5855 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5856 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5857 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5858 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
  apache 5859 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart

腳本 改進(jìn)

  [root@oldboy-B /]# echo oldboytest >/var/www/html/index.htm
  [root@oldboy-B /]# wget –quiet –spider http://10.0.0.161/index.htm
  [root@oldboy-B /]# echo $?
  0
  [root@oldboy-B /]# ll index.htm
  ls: index.htm: 沒(méi)有那個(gè)文件或目錄
  [root@oldboy-B scripts]# cat apachemon1
  #!/bin/sh
  #created by oldboy 20110523
  
  . /etc/init.d/functions
  #HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l` #=====>這個(gè)是基于http方式進(jìn)行判斷
  wget –quiet –spider http://10.0.0.161/index.htm #=====>這個(gè)是基于WGET URL方式進(jìn)行判斷
  if [ $? -ne 0 ];then
  action “httpd is not running” /bin/false >/tmp/httpd.log
  httpdctl restart >/dev/null 2>&1
  action “httpd is restart” /bin/true >>/tmp/httpd.log
  mail -s “`uname -n`’s httpd restarted at `(date)`” mail@qq.com
  exit 1
  else
  action “httpd is running” /bin/true
  exit 0
  fi

真正使用時(shí),有些輸出是不需要的就去掉

  [root@oldboy-B scripts]# cat apachemon1
  #!/bin/sh
  #created by oldboy 20110523
  #
  . /etc/init.d/functions
  wget –quiet –spider http://10.0.0.161/index.htm #=====>這個(gè)是基于WGET URL方式進(jìn)行判斷
  if [ $? -ne 0 ];then
  action “httpd is not running” /bin/false >/tmp/httpd.log
  httpdctl restart >/dev/null 2>&1
  action “httpd is restart” /bin/true >>/tmp/httpd.log
  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com
  exit 1
  fi

多條件判斷的寫(xiě)法

  [root@oldboy-B scripts]# cat apachemon1
  #!/bin/sh
  #created by oldboy 20110523
  #
  . /etc/init.d/functions
  HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l`
  HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
  wget –quiet –spider http://10.0.0.161/index.htm && RETVAL=$?
  if [ $RETVAL -ne 0 ] || [ $HTTPPORTNUM -ne 1 ] || [ $HTTPPRONUM -lt 1 ] ;then
  #if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" != "1" -o "$HTTPPRONUM" \< "1" ] ;then
  action “httpd is not running” /bin/false
  action “httpd is not running” /bin/false >/tmp/httpd.log
  httpdctl restart >/dev/null 2>&1
  action “httpd is restart” /bin/true
  mail -s “`uname -n`’s httpd restarted at `(date)`” 31333741@qq.com
  exit 1
  else
  action “httpd is running” /bin/true
  exit 0
  fi

以上是“shell腳本怎樣實(shí)現(xiàn)定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

當(dāng)前名稱(chēng):shell腳本怎樣實(shí)現(xiàn)定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article18/ddoigp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、手機(jī)網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)