Google Answers Logo
View Question
 
Q: Writing a shell script to confirm a site it up? ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Writing a shell script to confirm a site it up?
Category: Computers > Programming
Asked by: kmlawson-ga
List Price: $3.00
Posted: 17 Nov 2003 21:26 PST
Expires: 17 Dec 2003 21:26 PST
Question ID: 276934
I have a computer which is mostly online.  I want to write a shell
script for it (Mac OS X, but using Bash on the command line) to check
if a few of my websites, which are hosted on other servers around the
US are online.

I'm not very happy with free web site monitoring services so I want to
do this on my own.

The remote hosts do not support Ping, so I need to use some other
means (curl?) to confirm that they are online.  What sort of command
would I include in the shell script to confirm the site is online and
its contents can be downloaded?
Answer  
Subject: Re: Writing a shell script to confirm a site it up?
Answered By: wengland-ga on 18 Nov 2003 08:11 PST
Rated:5 out of 5 stars
 
Greetings!

You're on the right track, thinking of using curl.  curl has a variety
of exit codes that you can test against in a bash script.  The two
error conditions I think you'd be most interested in are:

6: Couldn't resolve host (DNS failure, or your hosting provider is gone)
7: Failed to connect to host 

A sample script is as follows for sh (same syntax as bash)

#! /usr/bin/sh

function check {
     if [ $? -ne 0 ] ; then
         echo "Error occurred getting URL $1:"
         if [ $? -eq 6 ]; then
             echo "Unable to resolve host"
         fi
         if [$? -eq 7 ]; then
             echo "Unable to connect to host"
         fi
         exit 1
     fi

}
curl -s -o "/dev/null" $1
check;

---- end script ----

In a nutshell, call the script with the HTTP URL to check.  curl will
silently try to get the file and send it to /dev/null.  If it fails,
it'll set the exit code, which is checked in the 'check' function.

You may have to play with the curl parameters to make it work just how
you want it.  You can also redirect the curl output to a temp file,
and use grep and filesize checks to make sure the content is correct. 
Read the man page for details.

Good luck!


Search Strategy:

man curl
://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=man+curl&btnG=Google+Search

bash check exit code
://www.google.com/search?q=bash+check+exit+code&sa=Google+Search

bash +if syntax
://www.google.com/search?q=bash+%2Bif+syntax&hl=en&lr=&ie=UTF-8&start=10&sa=N




Further Reading:

curl man page
http://www.hmug.org/man/1/curl.html

bash reference manual
http://www.faqs.org/docs/bashman/bashref.html#SEC_Top
kmlawson-ga rated this answer:5 out of 5 stars and gave an additional tip of: $1.00
Your answer was thorough and even gave me an example script

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy