Linux script with curl to check webservice is up

I use:

curl -f -s -I "http://example.com" &>/dev/null && echo OK || echo FAIL

-f --fail Fail silently (no output at all) on HTTP errors
-s --silent Silent mode
-I --head Show document info only

Note:
depending on needs you can also remove the "-I" because in some cases you need to do a GET and not a HEAD


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
200

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


Same as @burhan-khalid, but added --connect-timeout 3 and --max-time 5.

test_command='curl -sL \
    -w "%{http_code}\\n" \
    "http://www.google.com:8080/" \
    -o /dev/null \
    --connect-timeout 3 \
    --max-time 5'
if [ $(test_command) == "200" ] ; 
then
   echo "OK" ;
else
   echo "KO" ;
fi

Tags:

Linux

Curl