Bash

Check URL Returns 200

admin by @admin ADMIN
3d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Quick health-check helper: returns 0 if a URL responds with a 2xx status, non-zero otherwise. Use in pre-deploy smoke tests.
Bash
Raw
check_url() {
    local url="$1" expected="${2:-200}"
    local code
    code="$(curl -sS -o /dev/null -w "%{http_code}" --connect-timeout 5 --max-time 10 "$url")"
    if [[ "$code" == "$expected" ]]; then
        return 0
    else
        echo "FAIL $url got $code (expected $expected)" >&2
        return 1
    fi
}

check_url https://savesnippets.com           && echo "homepage OK"
check_url https://savesnippets.com/login.php && echo "login OK"

# Wait until a service comes up (handy in docker-compose start scripts)
wait_for_url() {
    local url="$1" timeout="${2:-60}"
    local start; start=$(date +%s)
    until check_url "$url" 2>/dev/null; do
        if (( $(date +%s) - start > timeout )); then
            echo "$url never came up in $timeout s" >&2; return 1
        fi
        sleep 1
    done
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.