BASH
Checking Website and API Health Status
A bash script to monitor the HTTP status of multiple web services or API endpoints, providing quick feedback on their availability and response codes.
#!/bin/bash
# List of URLs to check
URLS=(
"https://www.google.com"
"https://api.example.com/health"
"http://localhost:3000"
"https://non-existent-domain-test.com"
)
echo "--- Checking URL Health Status ---"
echo "----------------------------------"
for url in "${URLS[@]}"; do
status_code=$(curl -s -o /dev/null -w "%{\http_code}" "$url")
if [ "$status_code" -eq 200 ]; then
echo "[OK] $url (Status: $status_code)"
elif [ "$status_code" -eq 000 ]; then
echo "[FAIL] $url (Connection refused or host not found)"
else
echo "[WARNING] $url (Status: $status_code)"
fi
done
echo "----------------------------------"
echo "Health check complete."
How it works: This script iterates through a predefined list of URLs, using `curl` to perform a non-verbose request (`-s -o /dev/null`) and print only the HTTP status code (`-w "%{http_code}"`). It then checks the returned status code: 200 for success, 000 for connection issues or host not found, and any other code as a warning. This provides a quick way to monitor the health of websites and API endpoints.