#!/bin/bash # YG_FT_Base 统一启动脚本 # 同时启动后端 API 服务、Web 静态服务器和 TensorBoard # 使用方法: bash start_all.sh # 自动修复脚本换行符 if grep -q $'\r' "$0"; then echo "检测到 Windows 换行符,自动修复中..." sed -i 's/\r$//' "$0" echo "修复完成,重新执行脚本..." exec "$0" fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" echo "====================================" echo "YG_FT_Base 统一启动脚本" echo "====================================" echo "" # 读取配置 CONFIG_FILE="$SCRIPT_DIR/config.yaml" if [ -f "$CONFIG_FILE" ]; then API_PORT=$(python3 -c "import yaml; print(yaml.safe_load(open('$CONFIG_FILE'))['app'].get('port', 7861))" 2>/dev/null) WEB_PORT=$(python3 -c "import yaml; print(yaml.safe_load(open('$CONFIG_FILE'))['app'].get('web_port', 7862))" 2>/dev/null) [ -z "$API_PORT" ] && API_PORT=7861 [ -z "$WEB_PORT" ] && WEB_PORT=7862 else API_PORT=7861 WEB_PORT=7862 fi echo "📦 端口配置:" echo " - 后端 API: $API_PORT" echo " - Web 服务器: $WEB_PORT" echo " - TensorBoard: 6006" echo "" # 检查端口是否已被占用 check_port() { if lsof -i:$1 &> /dev/null || netstat -tuln | grep -q ":$1 " 2>/dev/null; then return 1 fi return 0 } # 启动后端 API 服务 start_api() { echo "🚀 启动后端 API 服务..." if [ -f "requirements.txt" ]; then if [ -d "B_venv" ]; then source B_venv/bin/activate else echo "⚠️ 虚拟环境不存在,请先运行 create_venv.sh 创建" return 1 fi fi # 检查端口 if ! check_port $API_PORT; then echo "❌ 端口 $API_PORT 已被占用,后端服务可能已在运行" return 1 fi LOG_DIR="$SCRIPT_DIR/logs/$(date +%Y-%m-%d)" mkdir -p "$LOG_DIR" python src/main.py > "$LOG_DIR/api.log" 2>&1 & API_PID=$! echo "✅ 后端服务已启动 (PID: $API_PID, 端口: $API_PORT)" echo "$API_PID" > /tmp/ygft_api.pid } # 启动 TensorBoard 服务 start_tensorboard() { echo "" echo "🚀 启动 TensorBoard 服务..." # 检查端口 if ! check_port 6006; then echo "⚠️ 端口 6006 已被占用,TensorBoard 可能已在运行" return 0 fi # 确保日志目录存在 LOG_DIR="/app/base/saves" if [ ! -d "$LOG_DIR" ]; then LOG_DIR="$SCRIPT_DIR/saves" fi if [ ! -d "$LOG_DIR" ]; then echo "⚠️ 日志目录不存在,跳过 TensorBoard 启动" return 0 fi # 启动 TensorBoard(后台运行) nohup tensorboard --logdir "$LOG_DIR" --port 6006 --bind_all > "$LOG_DIR/tensorboard.log" 2>&1 & TB_PID=$! echo "✅ TensorBoard 服务已启动 (PID: $TB_PID, 端口: 6006)" echo "$TB_PID" > /tmp/ygft_tensorboard.pid echo "📊 TensorBoard 访问地址: http://localhost:6006" } # 启动 Web 静态服务器 start_web() { echo "" echo "🚀 启动 Web 静态服务器..." cd "$SCRIPT_DIR/web" # 检查端口 if ! check_port $WEB_PORT; then echo "⚠️ 端口 $WEB_PORT 已被占用,Web 服务可能已在运行" return 1 fi python3 -m http.server $WEB_PORT & WEB_PID=$! echo "✅ Web 服务已启动 (PID: $WEB_PID, 端口: $WEB_PORT)" echo "$WEB_PID" > /tmp/ygft_web.pid } # 停止服务 stop_all() { echo "" echo "🛑 停止所有服务..." if [ -f /tmp/ygft_api.pid ]; then kill $(cat /tmp/ygft_api.pid) 2>/dev/null rm /tmp/ygft_api.pid echo "✅ 后端服务已停止" fi if [ -f /tmp/ygft_web.pid ]; then kill $(cat /tmp/ygft_web.pid) 2>/dev/null rm /tmp/ygft_web.pid echo "✅ Web 服务已停止" fi if [ -f /tmp/ygft_tensorboard.pid ]; then kill $(cat /tmp/ygft_tensorboard.pid) 2>/dev/null rm /tmp/ygft_tensorboard.pid echo "✅ TensorBoard 服务已停止" fi # 清理可能残留的进程 pkill -f "src/main.py" 2>/dev/null pkill -f "http.server $WEB_PORT" 2>/dev/null pkill -f "tensorboard.*6006" 2>/dev/null } # 显示状态 status() { echo "" echo "📊 服务状态:" echo "" if [ -f /tmp/ygft_api.pid ] && kill -0 $(cat /tmp/ygft_api.pid) 2>/dev/null; then echo "✅ 后端 API: 运行中 (PID: $(cat /tmp/ygft_api.pid), 端口: $API_PORT)" else echo "❌ 后端 API: 未运行" fi if [ -f /tmp/ygft_web.pid ] && kill -0 $(cat /tmp/ygft_web.pid) 2>/dev/null; then echo "✅ Web 服务: 运行中 (PID: $(cat /tmp/ygft_web.pid), 端口: $WEB_PORT)" else echo "❌ Web 服务: 未运行" fi if [ -f /tmp/ygft_tensorboard.pid ] && kill -0 $(cat /tmp/ygft_tensorboard.pid) 2>/dev/null; then echo "✅ TensorBoard: 运行中 (PID: $(cat /tmp/ygft_tensorboard.pid), 端口: 6006)" else echo "❌ TensorBoard: 未运行" fi echo "" echo "🌐 访问地址:" echo " - 后端 API: http://localhost:$API_PORT" echo " - Web 页面: http://localhost:$WEB_PORT/pages/main.html" echo " - TensorBoard: http://localhost:6006" } # 主菜单 case "$1" in start) start_api start_tensorboard start_web echo "" echo "====================================" echo "所有服务已启动!" echo "====================================" status ;; stop) stop_all echo "✅ 所有服务已停止" ;; restart) stop_all sleep 1 start_api start_tensorboard start_web echo "" echo "====================================" echo "所有服务已重启!" echo "====================================" status ;; status) status ;; *) echo "用法: $0 {start|stop|restart|status}" echo "" echo "命令:" echo " start - 启动所有服务" echo " stop - 停止所有服务" echo " restart - 重启所有服务" echo " status - 查看服务状态" exit 1 ;; esac