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
5 Answers
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
-
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.– LegoDec 19, 2013 at 17:20
-
2
-
2You 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.– sk29910Sep 18, 2019 at 17:06 -
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
-
1As of 2022 you will most likely have to use
grep "HTTP\/2\ 200"
since most servers will have switched to HTTP2 protocol Mar 28, 2022 at 20:04
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
-
This plugin doesn't have source, you should properly link this. The code is not known.– MaXi32Jul 31, 2021 at 5:03
-
This has been an official Nagios plugin since at least 2002, according to the git logs: github.com/nagios-plugins/nagios-plugins. It isn't exactly obscure. Aug 6, 2021 at 20:30
-
Do you mean this script can be used independently or are you suggesting to install this nagious thing just to solve the bash problem ?– MaXi32Aug 7, 2021 at 6:01
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).
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.
-
I don't think this works for https sites. For http yes, i get a exit status 0– LegoDec 19, 2013 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. Dec 19, 2013 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– LegoDec 19, 2013 at 17:21