#!/usr/bin/env bash # MaiBot 本机维护脚本(在 240 上执行) # 用法示例: # bash maibot_maintenance.sh stop # bash maibot_maintenance.sh status # bash maibot_maintenance.sh restart set -euo pipefail CONTAINER_NAME="${CONTAINER_NAME:-maibot-core-lite}" WEBUI_PORT="${WEBUI_PORT:-18001}" print_help() { cat <<'EOF' 用法: maibot_maintenance.sh 动作: status 查看容器状态 stop 停止 MaiBot 容器 start 启动 MaiBot 容器 restart 重启 MaiBot 容器 logs 查看最近日志(默认 10 分钟) health 检查 WebUI 健康接口 可选环境变量: CONTAINER_NAME 默认 maibot-core-lite WEBUI_PORT 默认 18001 EOF } action="${1:-}" if [[ -z "${action}" ]]; then print_help exit 1 fi case "${action}" in status) echo "[INFO] 容器状态:" docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}' | head -n 1 docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}' | grep "${CONTAINER_NAME}" || true ;; stop) echo "[INFO] 正在停止容器: ${CONTAINER_NAME}" docker stop "${CONTAINER_NAME}" echo "[INFO] 已停止" ;; start) echo "[INFO] 正在启动容器: ${CONTAINER_NAME}" docker start "${CONTAINER_NAME}" echo "[INFO] 已启动" ;; restart) echo "[INFO] 正在重启容器: ${CONTAINER_NAME}" docker restart "${CONTAINER_NAME}" echo "[INFO] 已重启" ;; logs) echo "[INFO] 最近 10 分钟关键日志:" docker logs --since 10m "${CONTAINER_NAME}" 2>&1 | grep -aE "Timing Gate|no_reply|reply|SendService|platform_map|Bridge received|发送|回复" || true ;; health) echo "[INFO] 检查健康接口: http://127.0.0.1:${WEBUI_PORT}/api/webui/health" curl -sS --max-time 8 "http://127.0.0.1:${WEBUI_PORT}/api/webui/health" || true echo ;; *) echo "[ERROR] 未知动作: ${action}" print_help exit 2 ;; esac