Bashbashmonitoringhealth-checkautomationproduction-ready
Service Health Check Script
A comprehensive health checking script that monitors HTTP endpoints, TCP ports, disk space, and service dependencies with alerting.
title: "Service Health Check Script" description: "A comprehensive health checking script that monitors HTTP endpoints, TCP ports, disk space, and service dependencies with alerting." category: "Bash" language: "bash" tags: ["bash", "monitoring", "health-check", "automation", "production-ready"] createdAt: "2025-02-05" updatedAt: "2025-02-25"
Overview
A comprehensive health check script that validates HTTP endpoints, TCP port availability, disk usage, and running processes. Failures trigger webhook alerts and are logged for historical analysis.
Code
check_http "API" "http://localhost:8080/health" 200
check_tcp "Database" "localhost" 5432
check_disk "/" 90
check_process "nginx" "nginx: master"Notes
curl -w "%{http_code}"extracts just the status code without body output/dev/tcpis a bash built-in for TCP connectivity testing (no netcat needed)dfwith awk extracts disk usage percentage for threshold comparison- Webhook alerts use fire-and-forget (|| true) to prevent alert failures from failing the script
- Run via cron every 30-60 seconds or as a systemd timer for continuous monitoring