shell指令碼檢查網站狀態

來源:互聯網
上載者:User

標籤:shell指令碼檢查網站狀態

檢查網站狀態通常使用wget或curl工具,下面分別使用這二種工具來做寫檢查網站的指令碼。(學習自老男孩shell編程)


命令列:

1、curl得到傳回值200,表示正常

[[email protected] ~]# curl -o /dev/null -s --connect-timeout 5 -w ‘%{http_code}‘ www.baidu.com

200[[email protected] ~]

2、wget得到0,表示正常

[[email protected] ~]# wget -T 5 -t 2 --spider www.baidu.com &>/dev/null

[[email protected] ~]# echo $?

0


指令碼:

1、culr

[[email protected] shell]# cat check_web1.sh

#!/bin/bash

#

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

array=(

http://www.china-cmd.org

http://www.cmdmedia.cn

http://www.icehtmc.com

https://mail.cmdmedia.cn

)

curl_ip() {

  CURL=$(curl -o /dev/null -s --connect-timeout 5 -w ‘%{http_code}‘ $1|egrep "200|302"|wc -l)

  return $CURL

}

main() {

   for n in ${array[*]}

   do 

      curl_ip $n

      if [ $? -eq 1 ];then

         action "curl $n" /bin/true

      else

         action "curl $n" /bin/false

         sleep 3

             CURL=$(curl_ip $n|egrep "200|302"|wc -l)

             if [ $CURL -eq 1 ];then

                action "Retry curl $n again" /bin/true

             else

                action "Retry curl $n again" /bin/false

             fi 

      fi

   done

} 

main

圖示:

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8D/BB/wKiom1ioYOWCw9-QAAFDKf159i8710.jpg" title="QQ20170218225738.jpg" alt="wKiom1ioYOWCw9-QAAFDKf159i8710.jpg" />


2、wget

[[email protected] shell]# cat check_web2.sh

#!/bin/bash

#

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

array=(

http://www.china-cmd.org

http://www.cmdmedia.cn

http://www.icehtmc.com

https://mail.cmdmedia.cn

)

curl_ip() {

  wget -T 5 -t 2 --spider $1 &>/dev/null

  return $?

}

main() {

   for n in ${array[*]}

   do 

      curl_ip $n

      if [ $? -eq 0 ];then

         action "curl $n" /bin/true

      else

         action "curl $n" /bin/false

      fi

   done

} 

main

圖示:

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8D/B9/wKioL1ioYWHxSX3dAAFI8IzODi8221.jpg" title="QQ20170218225959.jpg" alt="wKioL1ioYWHxSX3dAAFI8IzODi8221.jpg" />


學習自:

老男孩shell

本文出自 “趙東偉的部落格” 部落格,請務必保留此出處http://zhaodongwei.blog.51cto.com/4233742/1899145

shell指令碼檢查網站狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.