Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to do a health check of a service by calling a specific url on it. Feels like the simplest solution would be to use cron to do the check every minute or so. In case of errors, cron sends me an email.

I tried using cUrl for this but I can't get it to output messages only on errors. If I try to direct output to /dev/null, it prints out progress report.

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5559  100  5559    0     0   100k      0 --:--:-- --:--:-- --:--:--  106k

I tried looking through the curl options but I just can't find anything to suit the situation where you want it to be silent on success but make noise on errors.

Is there a way to make curl do what I want or is there some other tool I should be looking at?

share|improve this question
1  
How about using icinga or any other monitoring system? –  Stéphane Chazelas Jul 29 '13 at 21:12
    
I'm using this on a hobby project with limited resources on a shared host. I would love to use a monitoring system otherwise. Thanks for the tip anyways, I had not heard of icinga before –  palto Aug 4 '13 at 19:13
add comment

3 Answers 3

up vote 4 down vote accepted

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
share|improve this answer
    
-sS didn't output the error message for some reason. I had to add -f also. The correct working command seems to be curl -fsS http://example.org > /dev/null. That will not output anything when nothing is wrong but it will print the status code on errors which is fine by me. –  palto Jul 29 '13 at 15:50
    
Ok, added -f for future references. –  ahilsend Jul 29 '13 at 16:08
add comment

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" admin@thesite.com 

This is just an bad example for a simple cron script. Normally, you want to get only one mail if a website is down.

share|improve this answer
add comment

I think the simplest way to check if the site is alive then 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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