HTTP Health Check
Check URLs for expected status codes and response time.
$ webtoolist --script service-health-check
Check systemd services and HTTP endpoints in one pass with a non-zero exit on failure.
monitoringhealthsystemdhttpDependencies: systemctl, curl
#!/usr/bin/env bash
set -euo pipefail
fail=0
read -r -a services <<< "${SERVICES:-ssh nginx}"
read -r -a urls <<< "${URLS:-http://127.0.0.1/health}"
for unit in "${services[@]}"; do
state="$(systemctl is-active "$unit" 2>/dev/null || true)"
if [[ "$state" == active ]]; then
echo "ok service $unit"
else
echo "fail service $unit ($state)" >&2
fail=1
fi
done
for url in "${urls[@]}"; do
code="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 5 "$url" || echo 000)"
if [[ "$code" == 200 ]]; then
echo "ok http $url"
else
echo "fail http $url ($code)" >&2
fail=1
fi
done
exit "$fail"
SERVICES='ssh nginx' URLS='https://example.com/health' ./service-health-check
Related scripts
Check URLs for expected status codes and response time.
Exit non-zero when any mounted filesystem exceeds a usage threshold.
List systemd units that are failed or restarting, with recent log lines.
Print active/inactive state for a list of systemd services.