0

Hi I am trying to execute specific tests only if application is up and running (I am using docker), I am trying to achieve this with the help of bash script. What I am expecting is I need to run a loop until I receive 200 status from application, once I receive 200 script should move ahead and execute the test.I am trying bash script as follows

#!/bin/bash
urlstatus=0
until [ $urlstatus -ne 200 ]; do
urlstatus=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "http://localhost:8000/animals")
echo $urlstatus
done

Execute Test if application is up & running

Please let me know what is missing in the script.

Thanks

5
  • Could you please do mention what error you are getting here? or what is not working. Commented Jun 22, 2018 at 14:51
  • You should do While True: and then and if statement, when urlstatus == 200 run your code and exit. Commented Jun 22, 2018 at 14:57
  • It's always not equal to 200 on the very first time it's tested -- you initialize the value to 0, so the loop doesn't need to run at all! If you want your loop to run even once, initialize urlstatus=200, not urlstatus=0. Commented Jun 22, 2018 at 14:59
  • that said, if you want to wait for 200, that's not what this code does; it waits until the value isn't 200. Commented Jun 22, 2018 at 15:01
  • Worry about that ^M showing on the first (shebang) line. Commented Jun 22, 2018 at 15:02

1 Answer 1

5

-ne is the exact opposite of the test you actually want; to loop until the exit status is 200 you should have -eq, or even better (to avoid error messages from the comparison if a non-numeric value is present), =.

#!/bin/sh
fetchstatus() {
  curl \
    -o /dev/null \
    --silent \
    --head \
    --write-out '%{http_code}' \
    "http://localhost:8000/animals"
}

urlstatus=$(fetchstatus)          # initialize to actual value before we sleep even once
until [ "$urlstatus" = 200 ]; do  # until our result is success...
  sleep 1                         # wait a second...
  urlstatus=$(fetchstatus)        # then poll again.
done

But since curl can adjust its exit status to indicate whether a request was successful, you don't even need that. Use --fail, and you can branch directly:

#!/bin/sh
while :; do
  curl -sS --fail -o /dev/null "http://localhost:8000/animals") && break
  sleep 1 # actually give your server a little rest
done

The && break means that we break out of the loop only if the request was successful; the --fail argument to curl means that it only returns success if the server returned a non-erroneous exit status (such as 200).

Sign up to request clarification or add additional context in comments.

3 Comments

This could also just be until curl -sS --fail -o /dev/null "http://localhost:8000/animals"); sleep 1; done
Would 301 be considered a non-erroneous status?
@MiguelOrtiz, yes, unless you use -L and it redirects to something that fails. Frankly, though, any service that uses a 301 when it's not done initializing yet is misdesigned -- it's explicitly a "permanent redirection", not something that can be expected to go away after things finish booting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.