start.sh 1.67 KB
#!/bin/bash

cd "$(dirname "$0")/.."

SYNC_PID="sync.pid"
BRAND_SYNC_PID="brand_sync.pid"
API_PID="api.pid"

echo "=== 门店管理服务启动脚本 ==="

if [ -f "requirements.txt" ]; then
    echo "正在检查并安装依赖..."
    pip3.10 install -r requirements.txt > /dev/null 2>&1
fi

start_service() {
    local pid_file=$1
    local name=$2
    local cmd=$3
    local log_file=$4

    if [ -f "$pid_file" ] && ps -p $(cat "$pid_file") > /dev/null 2>&1; then
        echo "警告: $name 已在运行 (PID: $(cat $pid_file))。"
    else
        echo "正在启动 $name ..."
        nohup $cmd > "$log_file" 2>&1 &
        echo $! > "$pid_file"
        echo "$name 已启动 (PID: $!)"
    fi
}

start_service "$SYNC_PID" "门店同步服务" "python3 bin/run_sync.py" "logs/startup_sync.log"
start_service "$BRAND_SYNC_PID" "品牌同步服务" "python3 bin/run_brand_sync.py" "logs/startup_brand_sync.log"

if [ -f "$API_PID" ] && ps -p $(cat "$API_PID") > /dev/null 2>&1; then
    echo "警告: API 服务已在运行 (PID: $(cat $API_PID))。"
else
    echo "正在启动 API 接口服务 (FastAPI)..."
    if [ -f "config.ini" ]; then
        CONF_PORT=$(sed -n '/^\[api\]/,/^\[/p' config.ini | grep "^port =" | awk -F' = ' '{print $2}')
        CONF_HOST=$(sed -n '/^\[api\]/,/^\[/p' config.ini | grep "^host =" | awk -F' = ' '{print $2}')
    fi
    PORT=${CONF_PORT:-8000}
    HOST=${CONF_HOST:-0.0.0.0}

    echo "配置信息: $HOST:$PORT"
    nohup python3 bin/run_api.py --server --host "$HOST" --port "$PORT" > "logs/startup_api.log" 2>&1 &
    echo $! > "$API_PID"
    echo "API 服务已启动 (PID: $!)"
fi

echo "--------------------------"
echo "所有服务已成功启动!"