Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

Linux script with curl to check webservice is up

I have a webservice provided at http://localhost/test/testweb

I want to write a script to check if webservice is up with curl

If there a curl parameter given, returns 200 OK ok true false so that I can use it is if-else block in linux script

Upvotes: 42

Views: 70191

Answers (6)

Zibri
Zibri

Reputation: 9827

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

Upvotes: 8

Mohamed EL HABIB
Mohamed EL HABIB

Reputation: 632

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

Upvotes: 6

sudhir kumar mishra
sudhir kumar mishra

Reputation: 116

Use this:

curl -o $CURL_OUTPUT -s -w %{http_code}\\n%{time_total}\\n $URL > $TMP_FILE 2>&1
cat $TMP_FILE

Upvotes: -1

Michael Cole
Michael Cole

Reputation: 16217

I needed a better answer to this, so I wrote the script below.

The fakePhrase is used to detect ISP "Search Assist" adware HTTP resposnes.

#!/bin/bash

fakePhrase="verizon"
siteList=(
  'http://google.com'
  'https://google.com'
  'http://wikipedia.org'
  'https://wikipedia.org'
  'http://cantgettherefromhere'
  'http://searchassist.verizon.com'
)

exitStatus=0

function isUp {
  http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
  fakeResponse=`cat temp_isUp | grep $fakePhrase`
  if [ -n "$fakeResponse" ]; then
    http=$fakePhrase
  fi
  case $http in
  [2]*)
    ;;
  [3]*)
    echo 'Redirect'
    ;;
  [4]*)
    exitStatus=4
    echo "$1 is DENIED with ${http}"
    ;;
  [5]*)
    exitStatus=5
    echo "$1 is ERROR with ${http}"
    ;;
  *)
    exitStatus=6
    echo "$1 is NO RESPONSE with ${http}"
    ;;
  esac
}

for var in "${siteList[@]}"
do
  isUp $var
done

if [ "$exitStatus" -eq "0" ]; then
  echo 'All up'
fi

rm temp_isUp
exit $exitStatus

Upvotes: -1

Thomas Nordquist
Thomas Nordquist

Reputation: 643

That will check the headers via wget 2>&1pipes the stderr to stdout grep filters -O /dev/null just throws the content of the page

if [ "\`wget http://example.org/ -O /dev/null -S --quiet 2>&1 | grep '200 OK'\`" != "" ]; 
then 
   echo Hello; 
fi;

I know not curl, but still a solution

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174624

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.

Upvotes: 76

Related Questions