WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script service-health-check

Service Health Check

Check systemd services and HTTP endpoints in one pass with a non-zero exit on failure.

category Monitoring shell Bash 4+ updated 2026-09-26
monitoringhealthsystemdhttp
Safety notes
  • Read-only: queries state and makes HTTP GET requests.
  • Set both SERVICES and URLS to match the host; empty lists are allowed.
commands involved: ssh

Dependencies: systemctl, curl

Usage

  • Configure SERVICES and URLS in the environment or edit the defaults.
  • Exit code 1 means at least one check failed.

Script

#!/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"

Example

SERVICES='ssh nginx' URLS='https://example.com/health' ./service-health-check

Notes

  • Run it from an external monitor as well, so a dead host is noticed.
  • HTTP 301/302 counts as failure here; adjust the accepted code for redirecting endpoints.

Related scripts

HTTP Health Check

Check URLs for expected status codes and response time.

Networking

Disk Threshold Alert

Exit non-zero when any mounted filesystem exceeds a usage threshold.

Monitoring

Failed Service Report

List systemd units that are failed or restarting, with recent log lines.

System Administration