Shell Script checks Site status

Source: Internet
Author: User

Check the site status usually using the wget or Curl tool, the following two tools are used to write the script to check the Web site. (Learning from the old boy shell programming)


Command line:

1. Curl gets a return value of 200, indicating normal

[Email protected] ~]# Curl-o/dev/null-s--connect-timeout 5-w '%{http_code} ' www.baidu.com

200[[email protected] ~]

2, wget get 0, indicating normal

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

[[email protected] ~]# echo $?

0


Script:

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

Icon:

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 $ &>/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

Icon:

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


Learn from:

Old boy Shell

This article is from the "Zhao Dongwei blog" blog, make sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1899145

Shell Script checks Site status

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.