Files
AIRegulation-DocAnalysis/start_api_background.sh
2026-04-28 11:29:33 +08:00

77 lines
1.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# start_api_background.sh - 后台启动API服务生产环境支持虚拟环境
set -e
VENV_DIR=".venv"
# 创建日志目录
mkdir -p logs
PID_FILE=logs/api.pid
LOG_FILE=logs/api.log
echo "========================================"
echo "后台启动 AI+合规智能中枢 API服务"
echo "========================================"
echo ""
# 检查虚拟环境
if [ ! -d "$VENV_DIR" ]; then
echo "错误: 虚拟环境不存在,请先运行 ./quick_start.sh"
exit 1
fi
# 检查是否已运行
if [ -f "$PID_FILE" ]; then
PID=$(cat $PID_FILE)
if ps -p $PID > /dev/null 2>&1; then
echo "服务已在运行 (PID: $PID)"
echo "如需重启,请先运行: ./stop_api.sh"
exit 1
else
rm -f $PID_FILE
fi
fi
# 启动参数
HOST=${API_HOST:-0.0.0.0}
PORT=${API_PORT:-8000}
echo "服务地址: http://$HOST:$PORT"
echo "日志文件: $LOG_FILE"
echo ""
# 后台启动(使用虚拟环境)
echo "正在后台启动..."
nohup $VENV_DIR/bin/python -m uvicorn src.api.main:app --host $HOST --port $PORT > $LOG_FILE 2>&1 &
PID=$!
# 保存PID
echo $PID > $PID_FILE
# 等待服务启动
sleep 3
# 检查是否启动成功
if ps -p $PID > /dev/null 2>&1; then
echo "服务启动成功 (PID: $PID)"
echo ""
echo "API地址: http://$HOST:$PORT"
echo "API文档: http://$HOST:$PORT/docs"
echo "健康检查: http://$HOST:$PORT/health"
echo ""
echo "前端测试页面:"
echo " 直接打开: frontend/index.html"
echo " 或启动服务: ./start_frontend.sh"
echo ""
echo "查看日志:"
echo " tail -f $LOG_FILE"
echo ""
echo "停止服务:"
echo " ./stop_api.sh"
else
echo "服务启动失败,请查看日志: $LOG_FILE"
rm -f $PID_FILE
exit 1
fi