Skip to content

Instantly share code, notes, and snippets.

@jimeh
Last active May 25, 2023 09:15
Show Gist options
  • Save jimeh/03019b4d7d0bbfccaa06997940c2d2b3 to your computer and use it in GitHub Desktop.
Save jimeh/03019b4d7d0bbfccaa06997940c2d2b3 to your computer and use it in GitHub Desktop.

Revisions

  1. jimeh revised this gist Aug 16, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wait-for.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@

    check_http() {
    wget -T 1 -S -q -O - "$1" 2>&1 | head -1 |
    head -1 | grep -E 'HTTP.+\s2\d{2}' > /dev/null 2>&1
    grep -E 'HTTP.+\s2[0-9]{2}' > /dev/null 2>&1
    return $?
    }

    @@ -67,4 +67,4 @@ if [ -n "$WAIT_FOR_TARGETS" ]; then
    done
    fi

    exec "$@"
    exec "$@"
  2. jimeh revised this gist Aug 16, 2021. 1 changed file with 16 additions and 7 deletions.
    23 changes: 16 additions & 7 deletions wait-for.sh
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@
    [ -n "$DEBUG" ] && set -x

    check_http() {
    wget -T 1 -S -q -O - "$1" 2>&1 | head -1 | \
    head -1 | grep -E 'HTTP.+\s2\d{2}' >/dev/null 2>&1
    wget -T 1 -S -q -O - "$1" 2>&1 | head -1 |
    head -1 | grep -E 'HTTP.+\s2\d{2}' > /dev/null 2>&1
    return $?
    }

    @@ -15,7 +15,7 @@ check_tcp() {
    exit 2
    fi

    nc -z -w1 "$host" "$port" >/dev/null 2>&1
    nc -z -w1 "$host" "$port" > /dev/null 2>&1
    return $?
    }

    @@ -24,14 +24,24 @@ wait_for() {
    uri="$2"
    timeout="${3:-30}"

    if [ "$type" = "tcp" ] && [ -z "$(which nc)" ]; then
    echo "ERROR: nc command not available" >&2
    exit 3
    fi

    if [ "$type" = "http" ] && [ -z "$(which wget)" ]; then
    echo "ERROR: wget command not available" >&2
    exit 3
    fi

    seconds=0
    while [ "$seconds" -lt "$timeout" ] && ! "check_${type}" "$uri"; do
    if [ "$seconds" -lt "1" ]; then
    printf "Waiting for %s ." "$uri"
    else
    printf .
    fi
    seconds=$((seconds+1))
    seconds=$((seconds + 1))
    sleep 1
    done

    @@ -48,14 +58,13 @@ wait_for() {

    if [ -n "$WAIT_FOR_TARGETS" ]; then
    uris="$(echo "$WAIT_FOR_TARGETS" | sed -e 's/\s+/\n/g' | uniq)"

    for uri in $uris; do
    if echo "$uri" | grep -E '^https?://.*' >/dev/null 2>&1; then
    if echo "$uri" | grep -E '^https?://.*' > /dev/null 2>&1; then
    wait_for "http" "$uri" "$WAIT_FOR_TIMEOUT"
    else
    wait_for "tcp" "$uri" "$WAIT_FOR_TIMEOUT"
    fi
    done
    fi

    exec "$@"
    exec "$@"
  3. jimeh created this gist Mar 13, 2020.
    61 changes: 61 additions & 0 deletions wait-for.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/bin/sh
    [ -n "$DEBUG" ] && set -x

    check_http() {
    wget -T 1 -S -q -O - "$1" 2>&1 | head -1 | \
    head -1 | grep -E 'HTTP.+\s2\d{2}' >/dev/null 2>&1
    return $?
    }

    check_tcp() {
    host="$(echo "$1" | cut -d: -f1)"
    port="$(echo "$1" | cut -d: -f2)"
    if [ -z "${host}" ] || [ -z "${port}" ]; then
    echo "TCP target ${1} is not in \"<host>:<port>\" format" >&2
    exit 2
    fi

    nc -z -w1 "$host" "$port" >/dev/null 2>&1
    return $?
    }

    wait_for() {
    type="$1"
    uri="$2"
    timeout="${3:-30}"

    seconds=0
    while [ "$seconds" -lt "$timeout" ] && ! "check_${type}" "$uri"; do
    if [ "$seconds" -lt "1" ]; then
    printf "Waiting for %s ." "$uri"
    else
    printf .
    fi
    seconds=$((seconds+1))
    sleep 1
    done

    if [ "$seconds" -lt "$timeout" ]; then
    if [ "$seconds" -gt "0" ]; then
    echo " up!"
    fi
    else
    echo " FAIL"
    echo "ERROR: unable to connect to: $uri" >&2
    exit 1
    fi
    }

    if [ -n "$WAIT_FOR_TARGETS" ]; then
    uris="$(echo "$WAIT_FOR_TARGETS" | sed -e 's/\s+/\n/g' | uniq)"

    for uri in $uris; do
    if echo "$uri" | grep -E '^https?://.*' >/dev/null 2>&1; then
    wait_for "http" "$uri" "$WAIT_FOR_TIMEOUT"
    else
    wait_for "tcp" "$uri" "$WAIT_FOR_TIMEOUT"
    fi
    done
    fi

    exec "$@"