Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wait for an HTTP endpoint to return 200 OK with Bash and curl
bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:9000)" != "200" ]]; do sleep 5; done'
# also check https://gist.github.com/rgl/c2ba64b7e2a5a04d1eb65983995dce76
@wilk

This comment has been minimized.

Copy link

@wilk wilk commented Mar 14, 2019

timeout 300 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:9000)" != "200" ]]; do sleep 5; done' || false

version with 5 minutes of timeout: if timeout is reached, it exits with error (1)

@neonidian

This comment has been minimized.

Copy link

@neonidian neonidian commented May 10, 2019

For TLS add --insecure flag for self signed certificates
timeout 300 bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' https://localhost:9000)" != "200" ]]; do sleep 5; done'

@bukowa

This comment has been minimized.

Copy link

@bukowa bukowa commented Jan 21, 2020

How to print status code before sleep?

@aaronArinder

This comment has been minimized.

Copy link

@aaronArinder aaronArinder commented Jan 24, 2020

This is awesome--thanks!

@bukowa

This comment has been minimized.

Copy link

@bukowa bukowa commented Jan 26, 2020

Here a script with timeout

#!/bin/bash
set -eux

declare -r HOST="http://google.com"

wait-for-url() {
    echo "Testing $1"
    timeout -s TERM 45 bash -c \
    'while [[ "$(curl -s -o /dev/null -L -w ''%{http_code}'' ${0})" != "200" ]];\
    do echo "Waiting for ${0}" && sleep 2;\
    done' ${1}
    echo "OK!"
    curl -I $1
}
wait-for-url http://${HOST}

Edit: this one actually works

#!/bin/bash
set -eux

declare HOST=$1
declare STATUS=$2
declare TIMEOUT=$3

HOST=$HOST STATUS=$STATUS timeout --foreground -s TERM $TIMEOUT bash -c \
    'while [[ ${STATUS_RECEIVED} != ${STATUS} ]];\
        do STATUS_RECEIVED=$(curl -s -o /dev/null -L -w ''%{http_code}'' ${HOST}) && \
        echo "received status: $STATUS_RECEIVED" && \
        sleep 1;\
    done;
    echo success with status: $STATUS_RECEIVED'
@snoby

This comment has been minimized.

Copy link

@snoby snoby commented May 4, 2020

Here a script with timeout

#!/bin/bash
set -eux

declare -r HOST="http://google.com"

wait-for-url() {
    echo "Testing $1"
    timeout -s TERM 45 bash -c \
    'while [[ "$(curl -s -o /dev/null -L -w ''%{http_code}'' ${0})" != "200" ]];\
    do echo "Waiting for ${0}" && sleep 2;\
    done' ${1}
    echo "OK!"
    curl -I $1
}
wait-for-url http://${HOST}

Testing http://localhost:8000
-bash: timeout: command not found
OK!
i don't think that the timeout command is on macOS

@cec

This comment has been minimized.

Copy link

@cec cec commented Jul 3, 2020

Hi all,

I turned this into a script allowing for various of these behaviors to be configured via parameters.

I'm using it in our CI pipelines to make sure that APIs served by containers are alive before performing the next steps in the pipeline.

I'm very far from being a Bash expert and PR for improvements are very welcome.

Of course feel free the fork if you wish to :)

@wonderflow

This comment has been minimized.

Copy link

@wonderflow wonderflow commented Dec 7, 2020

Thanks! Hope someone could make it to be a github action.

@phillbaker

This comment has been minimized.

Copy link

@phillbaker phillbaker commented Dec 21, 2020

There is https://github.com/nev7n/wait_for_response, however, that doesn't support all the options available in curl.

@RogerRey14

This comment has been minimized.

Copy link

@RogerRey14 RogerRey14 commented Jun 9, 2021

Hi all,

Here a solution that worked to me. In my case I used it for an initContainer:

sh -c 'while (( curl -s -o /dev/null -w %{http_code} https://google.com/ != "200" && curl -s -o /dev/null -w %{http_code} https://google.com/ != "301")); do sleep 1; done;'

@bukowa

This comment has been minimized.

Copy link

@bukowa bukowa commented Jun 9, 2021

#!/bin/bash
set -eux

declare HOST=$1
declare STATUS=$2
declare TIMEOUT=$3

HOST=$HOST STATUS=$STATUS timeout --foreground -s TERM $TIMEOUT bash -c \
    'while [[ ${STATUS_RECEIVED} != ${STATUS} ]];\
        do STATUS_RECEIVED=$(curl -s -o /dev/null -L -w ''%{http_code}'' ${HOST}) && \
        echo "received status: $STATUS_RECEIVED" && \
        sleep 1;\
    done;
    echo success with status: $STATUS_RECEIVED'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment