Linux script with curl to check webservice is up

后端 未结 6 795
难免孤独
难免孤独 2021-01-30 17:05

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 parame

相关标签:
6条回答
  • 2021-01-30 17:15

    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

    0 讨论(0)
  • 2021-01-30 17:15

    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

    0 讨论(0)
  • 2021-01-30 17:23

    Use this:

    curl -o $CURL_OUTPUT -s -w %{http_code}\\n%{time_total}\\n $URL > $TMP_FILE 2>&1
    cat $TMP_FILE
    
    0 讨论(0)
  • 2021-01-30 17:31

    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
    
    0 讨论(0)
  • 2021-01-30 17:38

    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
    
    0 讨论(0)
  • 2021-01-30 17:39
    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.

    0 讨论(0)
提交回复
热议问题