1

I'm doing a script to monitor some things from websites and one of the things is to monitor the http status and their response time.

In the script I run a command to get the http_status: (http command is provided by: httpie : A Curl-like tool for humans)

http -timeout 10 -follow -h http: //$I/ | grep "HTTP\/1.1" | awk '{print $2}'

This command will return the status itself, ie: 200, 404, 403, etc. or will return two other things:

http: error: Request timed out (10.0s)

or

http: error: ConnectionError ..."

Note: Increasing the timeout does not solve my problem. I need it to be 10 seconds.

How do I put a specific code when returning these two other options? For example in the timeout return 9999 and error 8888.

  • Just to know, I want the returned status to be the site itself (200,302, 403, etc) and in case of error return some number so that through a dashboard can mount the status of all my sites. So would have the: Site xxx.com = status 200 Site xyx.org = status 900 etc – Jorge Aug 17 '17 at 18:45
0

A simple case statement should do what you want:

case "$(http_status)" in
    "case 1")
        return 199
        ;;
    "case 2")
        return 198
        ;;
    *)
        # All is well; do nothing
        ;;
esac

You can use wildcards, e. g. *"timed out"*) or *"ConnectionError"*).

  • what i do wrong? (forced a wrong url) http_code=$(http --timeout 10 --follow -h wwwwww.google.com.br | grep "HTTP\/1.1" | awk '{print $2}') case "($http_code)" in "timed out") return 8888 echo 8888 ;; "ConnectionError") return 9999 echo 9999 ;; *) echo $http_code ;; esac ~ – Jorge Aug 17 '17 at 16:51
  • Return codes cannot AFAIK be higher than 255. – DopeGhoti Aug 17 '17 at 16:56
  • Changed return code to 8 and 9 respectively. but script cant get the status returned by programm. – Jorge Aug 17 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.