Health check of web page using curl
What about -sSf
? From the man pages:
-s/--silent Silent or quiet mode. Do not show progress meter or error messages. Makes Curl mute. -S/--show-error When used with -s it makes curl show an error message if it fails. -f/--fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when a HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22. This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407).
For example:
curl -sSf http://example.org > /dev/null
I think that for the simplest way to check if the site is alive, you could use the following method:
curl -Is http://www.google.com | head -n 1
This will return HTTP/1.1 200 OK
. If the return doesn't match your output then call out for help.
You need the -s
flag (silent), -f
flag (fail with exit code on error) and can use the -o
flag to redirect output:
curl www.websiteToTest.com -s -f -o /dev/null || echo "Website down." | mail -s "Website is down" [email protected]
This is just an bad example for a simple cron script. Normally, you want to get only one mail if a website is down.