BASH
Monitor Website Health with Basic HTTP Check
A simple bash script to perform a basic HTTP GET request to a URL and verify its availability by checking the HTTP status code, ideal for quick website health checks.
#!/bin/bash
# Basic website health check using curl
URL="https://example.com"
EXPECTED_STATUS="200"
TIMEOUT_SECONDS=10
echo "Checking health of $URL..."
HTTP_STATUS=$(curl -o /dev/null -s -w "%^{http_code}
" --max-time "$TIMEOUT_SECONDS" "$URL")
if [ "$HTTP_STATUS" == "$EXPECTED_STATUS" ]; then
echo "Health check PASSED: $URL returned HTTP $HTTP_STATUS"
exit 0
else
echo "Health check FAILED: $URL returned HTTP $HTTP_STATUS (Expected: $EXPECTED_STATUS)"
exit 1
fi
How it works: This script uses `curl` to make a GET request to a specified URL. It retrieves only the HTTP status code, discarding the body. The script then compares the returned status code against an `EXPECTED_STATUS` (e.g., 200 for success) and reports whether the health check passed or failed, exiting with an appropriate status code for automation and monitoring systems.