#!/bin/bash # ============================================================ # startup.sh — 小唯自启动脚本 # 开机或 session 初始化时调用 # 确保关键服务和基础设施已就绪 # ============================================================ set -e LOG="$HOME/.hermes/startup.log" echo "=== 小唯自启动 $(date) ===" > "$LOG" log() { echo "[$(date +%H:%M:%S)] $*" | tee -a "$LOG"; } # 1. 确保 systemd user services 运行 log "检查 systemd 用户服务..." for svc in zhiyid.service bge-embed.service; do if systemctl --user is-active "$svc" &>/dev/null; then log " ✅ $svc 已在运行" else log " ⚠️ $svc 未运行,尝试启动..." systemctl --user start "$svc" 2>&1 | tee -a "$LOG" || true sleep 3 if systemctl --user is-active "$svc" &>/dev/null; then log " ✅ $svc 启动成功" else log " ❌ $svc 启动失败" fi fi done # 2. 确保 NewAPI 运行 log "检查 NewAPI..." if pgrep -f "new-api" > /dev/null 2>&1; then log " ✅ NewAPI 运行中" else log " ⚠️ NewAPI 未运行,尝试启动..." if [ -f /usr/local/bin/new-api ]; then nohup /usr/local/bin/new-api > /dev/null 2>&1 & sleep 2 if pgrep -f "new-api" > /dev/null 2>&1; then log " ✅ NewAPI 启动成功" else log " ❌ NewAPI 启动失败" fi else log " ❌ NewAPI 未安装" fi fi # 3. 确保 Playwright REST API 运行 log "检查 Playwright REST API..." if curl -sf http://localhost:3333/health > /dev/null 2>&1; then log " ✅ Playwright API 响应正常" elif pgrep -f "playwright_rest" > /dev/null 2>&1; then log " ⚠️ Playwright API 进程在但无响应,重启..." pkill -f "playwright_rest" || true sleep 2 fi if ! curl -sf http://localhost:3333/health > /dev/null 2>&1; then if [ -f /tmp/playwright_rest_server.py ]; then log " 🔄 启动 Playwright API..." nohup python3 /tmp/playwright_rest_server.py > /dev/null 2>&1 & sleep 3 if curl -sf http://localhost:3333/health > /dev/null 2>&1; then log " ✅ Playwright API 启动成功" else log " ❌ Playwright API 启动失败" fi else log " ❌ Playwright API 脚本不存在" fi fi # 6. 检查服务器挂载 log "检查服务器挂载..." if mountpoint -q /mnt/server-backup; then log " ✅ 服务器已挂载" # 做一次快速备份 if [ -f "$HOME/.hermes/scripts/dual-backup.sh" ]; then bash "$HOME/.hermes/scripts/dual-backup.sh push" 2>&1 | tail -1 | log fi else log " ⚠️ 服务器未挂载(可能不在线)" fi log "检查网络连通性..." if curl -sf --max-time 5 https://api.deepseek.com > /dev/null 2>&1; then log " ✅ 外网可达" else log " ⚠️ 外网不可达(可能正常,取决于网络状态)" fi # 5. 运行一次快速健康检查 log "运行快速健康检查..." if [ -f "$HOME/.hermes/scripts/health-watchdog.sh" ]; then bash "$HOME/.hermes/scripts/health-watchdog.sh" 2>&1 | tee -a "$LOG" log " ✅ 健康检查完成" fi log "=== 自启动完成 ===" echo "=== 自启动完成 ===" >> "$LOG"