#!/bin/bash # 健康检查脚本:定期检查应用是否正常运行,如果不正常则自动重启 # 配置参数 APP_DIR="/app" DOCKER_COMPOSE_FILE="docker-compose.yml" CHECK_INTERVAL=300 # 检查间隔(秒) MAX_RESTARTS=3 # 最大重启次数 RESTART_INTERVAL=3600 # 重启间隔(秒) # 带颜色的日志函数 colored_log() { local color=$1 local message=$2 local reset="\033[0m" local colors=( ["red"]="\033[31m" ["green"]="\033[32m" ["yellow"]="\033[33m" ["blue"]="\033[34m" ["purple"]="\033[35m" ) echo -e "${colors[$color]}[$(date '+%Y-%m-%d %H:%M:%S')] $message$reset" } log() { colored_log "blue" "$1" } success() { colored_log "green" "$1" } error() { colored_log "red" "$1" } warning() { colored_log "yellow" "$1" } log "启动健康检查服务..." # 初始化重启计数器 restart_count=0 last_restart_time=$(date +%s) while true; do log "开始健康检查..." cd "$APP_DIR" # 检查容器是否在运行 if docker-compose -f "$DOCKER_COMPOSE_FILE" ps | grep -q "Up"; then # 检查应用是否能正常响应 if curl -s -o /dev/null -w "%{http_code}" http://localhost:3000 > /dev/null; then success "应用运行正常" else warning "应用容器在运行,但无法正常响应请求" # 查看应用日志 log "查看应用日志:" docker-compose -f "$DOCKER_COMPOSE_FILE" logs -n 10 # 检查重启次数 current_time=$(date +%s) time_since_last_restart=$((current_time - last_restart_time)) if [ $restart_count -lt $MAX_RESTARTS ] || [ $time_since_last_restart -gt $RESTART_INTERVAL ]; then log "尝试重启应用..." if docker-compose -f "$DOCKER_COMPOSE_FILE" restart; then success "应用已重启" restart_count=$((restart_count + 1)) last_restart_time=$current_time else error "应用重启失败" fi else error "已达到最大重启次数,在$RESTART_INTERVAL秒内不再尝试重启" fi fi else warning "应用容器未运行" # 检查重启次数 current_time=$(date +%s) time_since_last_restart=$((current_time - last_restart_time)) if [ $restart_count -lt $MAX_RESTARTS ] || [ $time_since_last_restart -gt $RESTART_INTERVAL ]; then log "尝试启动应用..." if docker-compose -f "$DOCKER_COMPOSE_FILE" up -d; then success "应用已启动" restart_count=$((restart_count + 1)) last_restart_time=$current_time else error "应用启动失败" fi else error "已达到最大重启次数,在$RESTART_INTERVAL秒内不再尝试启动" fi fi log "健康检查完成,等待$CHECK_INTERVAL秒后再次检查..." sleep $CHECK_INTERVAL done