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 figure curl would do the job. I wrote in a bash script:

#!/bin/sh

function test {
    res=`curl -I $1 | grep HTTP/1.1 | awk {'print $2'}`
    if [ $res -ne 200 ]
    then
        echo "Error $res on $1"
    fi
}  

test mysite.com
test google.com

The problem here is no matter what I do I can't get it to stop printing the below to stdout:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

I want a cronjob to run this script and if it writes such a message then everytime I run it I'll get an email because something has been printed to stdout in cron, even though the site may be fine.

How do I get the status code without getting junk into stdout? This code works except the bonus junk to the stdout preventing me from using it.

share|improve this question

4 Answers 4

up vote 4 down vote accepted
   -s/--silent
          Silent or quiet mode. Don't show progress meter 
          or error messages.  Makes Curl mute.

So your res should look like

res=`curl -s -I $1 | grep HTTP/1.1 | awk {'print $2'}`

Result is Error 301 on google.com, for example.

share|improve this answer
    
Thank you! this irratated me so much. (and they should mention this under the "progress meter" in the man page) –  acidzombie24 Dec 9 '11 at 13:22

Use this as your condition...

res=`curl -s --head <URL> | head -n 1 | grep -c HTTP/1.1 200 OK`

if [ $res -eq 1 ]
then
MSG = " OKAY"
EXIT_CODE = 0
else
MSG = " NOT OKAY"
EXIT_CODE = 2
fi
share|improve this answer

you want

...
res=$( curl -w %{http_code} -s --output /dev/null $1)
...

That will give you the HTTP_STATUS code as the only output.

share|improve this answer

I'd do it like this to ensure i get redirected to the final host and get the response from it:

res=($(curl -sLI "${1}" | grep '^HTTP/1' | tail -1))
((res[1] == 200)) || echo ${res[2]}
share|improve this answer

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.