#!/bin/bash ############################################## # Snapshot 后端服务状态检查脚本 # 适用于 Linux 环境 ############################################## # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } echo "============================================" echo "Snapshot 后端服务状态" echo "============================================" echo # 检查进程 echo "[1] 进程状态" echo "--------------------------------------------" PIDS=$(pgrep -f "snapshot-backend-1.0.0.jar") if [ -n "$PIDS" ]; then print_info "服务正在运行" echo "进程ID: $PIDS" echo ps -p $PIDS -o pid,ppid,cmd,%mem,%cpu,etime else print_warning "服务未运行" fi echo # 检查端口占用 echo "[2] 端口状态" echo "--------------------------------------------" if command_exists netstat; then PORT_INFO=$(netstat -tuln 2>/dev/null | grep :7626) if [ -n "$PORT_INFO" ]; then print_info "端口 7626 已被占用" echo "$PORT_INFO" else print_warning "端口 7626 未被占用" fi elif command_exists ss; then PORT_INFO=$(ss -tuln 2>/dev/null | grep :7626) if [ -n "$PORT_INFO" ]; then print_info "端口 7626 已被占用" echo "$PORT_INFO" else print_warning "端口 7626 未被占用" fi else print_warning "无法检查端口状态(缺少netstat和ss命令)" fi echo # 检查日志 echo "[3] 日志信息" echo "--------------------------------------------" if [ -f "app.log" ]; then print_info "日志文件存在: app.log" LOG_SIZE=$(du -h app.log | cut -f1) echo "文件大小: $LOG_SIZE" echo echo "最近20行日志:" echo "--------------------------------------------" tail -n 20 app.log echo "--------------------------------------------" echo echo "查看完整日志: tail -f app.log" else print_warning "日志文件不存在" fi echo # 健康检查 echo "[4] 健康检查" echo "--------------------------------------------" if command_exists curl; then HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:7626 2>/dev/null) if [ "$HTTP_CODE" = "200" ]; then print_info "HTTP健康检查通过 (200)" elif [ -n "$HTTP_CODE" ] && [ "$HTTP_CODE" != "000" ]; then print_warning "HTTP响应: $HTTP_CODE" else print_error "无法连接到服务" fi else print_warning "未找到curl命令,跳过HTTP健康检查" fi echo # JVM信息(如果进程在运行) if [ -n "$PIDS" ]; then echo "[5] JVM信息" echo "--------------------------------------------" if command_exists jps; then print_info "Java进程信息:" jps -lvm | grep snapshot fi echo fi echo "============================================"