7

I'm trying to setup another block of code to a bash script to check if a public website on HTTPS is up or not. Can we do this using CURL? Any suggestions what can be used besides CURL. Thanks

  • 1
    pingdom.com – ceejayoz Dec 19 '13 at 16:18
  • 2
    As Clippy might say, "It looks like you're trying to write a network monitoring system..." – Gerald Combs Dec 19 '13 at 16:45
4

Here is a way to do it using wget instead of curl. Keep in mind that MacOS doesn't come with wget by default.

A successful web request will return a code of 200, a failure will return a 300, 400, 404, ect... (see REST API codes)

This line will return a 1 if the web request was successful, otherwise it will return 0

wget -q  -O /tmp/foo google.com | grep '200' /tmp/foo | wc -l
1
| improve this answer | |
  • I'm aware of the multitude of applications out there to monitor websites but I wanted something simple to be included in my script. The wget -q option works great. Thank you. – Lego Dec 19 '13 at 17:20
  • 1
    HEAD google.com | grep '200\ OK' | wc -l avoids the temp file. – Frank Nocke Aug 22 '18 at 8:44
  • 1
    You can just use wget without the temp file: wget --spider --server-response google.com 2>&1 | grep '200\ OK' | wc -l . --server-response prints the headers, --spider prevents downloading the page itself. – sk29910 Sep 18 '19 at 17:06
  • wget is faster then HEAD in this case. – VladSavitsky Jul 9 at 22:17
15

One of many:

if curl -s --head  --request GET https://example.com | grep "200 OK" > /dev/null; then 
   echo "mysite.com is UP"
else
   echo "mysite.com is DOWN"
fi
| improve this answer | |
3

Nagios' check_http plugin can do this and much more, including checking for specific text in the response. You can run it from a shell script independently of Nagios itself:

$ check_http --ssl -H www.google.com -r 'Feeling Lucky'
HTTP OK: HTTP/1.1 200 OK - 11900 bytes in 0.086 second response time |time=0.085943s;;;0.000000 size=11900B;;;0

$ echo $?
0
| improve this answer | |
2

Why not use a full solution for monitoring? I've found monit to be pretty good for this: http://mmonit.com/monit/

(this comes after years of using home brewed bash scripts - i've found monit to be more transportable to different boxes, and more robust than some messy scripts. Don't know about their paid version).

| improve this answer | |
1

a similiar question was answered here:

https://stackoverflow.com/questions/12747929/linux-script-with-curl-to-check-webservice-is-up

The Qoute is from Burhan Khalid

curl -sL -w "%{http_code}\n" "http://www.google.com/" -o /dev/null

-s = Silent cURL's output

-L = Follow redirects

-w = Custom output format

-o = Redirects the HTML output to /dev/null

Example:

[~]$ curl -sL -w "%{http_code}\n" "http://www.google.com/" -o /dev/null

I would probably remove the \n if It were to capture the output.

so you just need to add -k in the options if you do not want to check for a valid certificate and use https instead of http obviously.

curl -sL -w "%{http_code}\n" "https://www.google.com/" -o /dev/null

reports the status code 200 with a return code of 0.

For everything else you need to define your response in the script.

| improve this answer | |
  • I don't think this works for https sites. For http yes, i get a exit status 0 – Lego Dec 19 '13 at 16:28
  • tested again. curl -sL -w "%{http_code}\n" "google.com" -o /dev/null;echo $? worked exactly at it should. you get the http code 200, and you get exit 0. changed formatting, there was at least on linebreak not transfered. – Dennis Nolte Dec 19 '13 at 16:30
  • It does work for google after the command was corrected. Thanks. However, it doesn't for our https site giving an output of 000. I'm using wget now – Lego Dec 19 '13 at 17:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.