Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux script with curl to check webservice is up

Tags:

linux

curl

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

like image 559
Ahmet Karakaya Avatar asked Oct 05 '12 14:10

Ahmet Karakaya


3 Answers

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.

like image 86
Burhan Khalid Avatar answered Nov 15 '22 07:11

Burhan Khalid


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

like image 39
Zibri Avatar answered Nov 15 '22 07:11

Zibri


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
like image 6
Mohamed EL HABIB Avatar answered Nov 15 '22 08:11

Mohamed EL HABIB