1

I would like to write a script to check whethere the application is up or not using unix shell scripts.

From googling I found a script wget -O /dev/null -q http://mysite.com, But not sure how this works. Can someone please explain. It will be helpful for me.

Selvakumar P
  • 305
  • 2
  • 8
  • 16

1 Answers1

3

wget is an HTTP client, so you're making a web request to http://mysite.com. You'll have to check the exit status of the wget command: if the request is successful, the exit status will be "0", and otherwise if it's not successful.

So, your check script may look something like:

#!/bin/bash

wget -O /dev/null -q http://mysite.com

if [ "$?" -ne "0" ]; then
  echo "Web site is down!"
fi

If you're not really sure about shell scripting, it might be better to use a service that will perform the check, along the lines of Pingdom.

Update:

Instead of using wget, it may be better to use curl. Something like this:

curl -sL -w "%{http_code}" http://mysite.com -o /dev/null

This will return the HTTP response code, so you can do more obvious controlled comparisons (i.e., curl will return 404, whereas wget will return some non-zero exit code that you have to figure out from documentation)

cjc
  • 24,916
  • 3
  • 51
  • 70
  • Thanks a lot cjc !!!, One more doubt how long it will wait for the response before exit? any idea?? – Selvakumar P Feb 15 '12 at 12:37
  • wget's timeout is by default 900 seconds. You can change this to something else with the `--timeout=seconds` option. What you should set this for depends on your website, how long you think a reasonable response to take, etc. Look at `man wget` for all the options. – cjc Feb 15 '12 at 12:43