本文目录导读:

我来介绍几种常见的Shell脚本健康检查端点实现方式:
HTTP健康检查(最常用)
#!/bin/bash
# 健康检查函数
health_check() {
local url="http://localhost:8080/health"
local timeout=5
local retry_count=3
local retry_interval=2
for ((i=1; i<=retry_count; i++)); do
echo "尝试第 $i 次健康检查..."
# 使用curl进行HTTP检查
response=$(curl -s -o /dev/null -w "%{http_code}" \
--connect-timeout $timeout \
--max-time $timeout \
"$url" 2>/dev/null)
if [ "$response" = "200" ]; then
echo "健康检查通过!HTTP状态码: $response"
return 0
fi
if [ $i -lt $retry_count ]; then
echo "检查失败,$retry_interval秒后重试..."
sleep $retry_interval
fi
done
echo "健康检查失败!"
return 1
}
# 执行检查
if health_check; then
echo "服务正常运行"
exit 0
else
echo "服务异常"
exit 1
fi
TCP端口健康检查
#!/bin/bash
tcp_health_check() {
local host="localhost"
local port=8080
local timeout=3
# 使用nc或bash内置的/dev/tcp
if timeout $timeout bash -c "echo > /dev/tcp/$host/$port" 2>/dev/null; then
echo "端口 $port 正常开放"
return 0
else
echo "端口 $port 连接失败"
return 1
fi
}
# 或者使用nc命令
tcp_health_check_nc() {
local host="localhost"
local port=8080
local timeout=3
if nc -z -w $timeout $host $port 2>/dev/null; then
echo "端口 $port 正常开放"
return 0
else
echo "端口 $port 连接失败"
return 1
fi
}
进程存在性检查
#!/bin/bash
process_health_check() {
local process_name="java"
local min_instances=1
# 检查进程是否存在
local process_count=$(ps aux | grep "$process_name" | grep -v grep | wc -l)
if [ "$process_count" -ge "$min_instances" ]; then
echo "进程 $process_name 运行正常 (实例数: $process_count)"
return 0
else
echo "进程 $process_name 未找到"
return 1
fi
}
# 检查Java进程并获取PID
check_java_process() {
local java_pid=$(pgrep -f "java.*your-application" | head -1)
if [ -n "$java_pid" ]; then
echo "Java应用运行中, PID: $java_pid"
return 0
else
echo "Java应用未运行"
return 1
fi
}
综合健康检查(多重指标)
#!/bin/bash
# 配置
APP_URL="http://localhost:8080"
APP_PORT=8080
PROCESS_NAME="java"
LOG_FILE="/var/log/app-health.log"
TIMEOUT=10
# 日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# HTTP检查
check_http() {
local response=$(curl -s -o /dev/null -w "%{http_code}" \
--connect-timeout $TIMEOUT \
--max-time $TIMEOUT \
"$APP_URL/health" 2>/dev/null)
if [ "$response" = "200" ]; then
log "HTTP检查通过,状态码: $response"
return 0
else
log "HTTP检查失败,状态码: $response"
return 1
fi
}
# TCP端口检查
check_tcp() {
if timeout $TIMEOUT bash -c "echo > /dev/tcp/localhost/$APP_PORT" 2>/dev/null; then
log "TCP检查通过,端口 $APP_PORT 正常"
return 0
else
log "TCP检查失败,端口 $APP_PORT 未开放"
return 1
fi
}
# 进程检查
check_process() {
local count=$(pgrep -c -f "$PROCESS_NAME")
if [ "$count" -gt 0 ]; then
log "进程检查通过,$PROCESS_NAME 进程数: $count"
return 0
else
log "进程检查失败,$PROCESS_NAME 进程未发现"
return 1
fi
}
检查
check_response_content() {
local response=$(curl -s --max-time $TIMEOUT "$APP_URL/health" 2>/dev/null)
# 检查JSON响应中的状态字段
if echo "$response" | grep -q '"status":"UP"' || \
echo "$response" | grep -q '"status":"ok"'; then
log "内容检查通过,服务状态正常"
return 0
else
log "内容检查失败,响应: $response"
return 1
fi
}
# 主检查函数
main_health_check() {
local failed=0
log "开始健康检查..."
# 执行各项检查
check_http || ((failed++))
check_tcp || ((failed++))
check_process || ((failed++))
check_response_content || ((failed++))
if [ "$failed" -eq 0 ]; then
log "所有健康检查通过 ✅"
return 0
else
log "有 $failed 项检查失败 ❌"
return 1
fi
}
# 执行主检查
main_health_check
exit_code=$?
# 发送告警(如果需要)
if [ $exit_code -ne 0 ]; then
# 这里可以添加发送邮件、Slack通知等
# ./send_alert.sh "服务健康检查失败"
echo "需要发送告警通知"
fi
exit $exit_code
带重试机制的检查
#!/bin/bash
health_check_with_retry() {
local max_retries=3
local retry_delay=5
local attempt=1
while [ $attempt -le $max_retries ]; do
echo "健康检查尝试 $attempt/$max_retries"
# 执行检查
if curl -sf "http://localhost:8080/health" > /dev/null 2>&1; then
echo "✅ 健康检查通过"
return 0
fi
if [ $attempt -lt $max_retries ]; then
echo "⚠️ 检查失败,${retry_delay}秒后重试..."
sleep $retry_delay
# 指数退避
retry_delay=$((retry_delay * 2))
fi
((attempt++))
done
echo "❌ 健康检查最终失败"
return 1
}
# 使用示例
if health_check_with_retry; then
echo "服务健康"
else
echo "服务异常,需要处理"
# 执行恢复操作
fi
使用方式
定时检查(crontab)
# 每5分钟执行一次健康检查
*/5 * * * * /path/to/health-check.sh
# 或使用while循环
#!/bin/bash
while true; do
./health-check.sh
sleep 300 # 5分钟
done
作为容器健康检查
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD /scripts/health-check.sh
Docker Compose配置
services:
app:
healthcheck:
test: ["CMD", "/scripts/health-check.sh"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
这些脚本可以根据实际需求进行调整和组合使用。