xuyonghao
2025-02-08 72a8a0a1ad6b79b8e9fb2facef121f9b5d584666
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
 
# 定义变量
VENV_PATH="./venv"  # 虚拟环境路径
APP_NAME="main:app"
HOST="0.0.0.0"
PORT="9201"
UVICORN_CMD="uvicorn $APP_NAME --host $HOST --port $PORT"
 
# 激活虚拟环境
source $VENV_PATH/bin/activate
 
# 停止现有进程
echo "Stopping existing processes..."
PIDS=$(pgrep -f "$UVICORN_CMD")
 
if [ -z "$PIDS" ]; then
    echo "No running processes found."
else
    for PID in $PIDS; do
        echo "Terminating process $PID..."
        kill -15 $PID
        sleep 1  # 等待1秒,确保进程有时间优雅关闭
        if kill -0 $PID > /dev/null 2>&1; then
            echo "Process $PID is still running, sending SIGKILL..."
            kill -9 $PID
        fi
    done
fi
 
# 启动新进程
echo "Starting new process..."
$UVICORN_CMD > server.log 2>&1 &
NEW_PID=$!
echo "New process started with PID $NEW_PID."
 
# 可选:检查新进程是否成功启动
sleep 2  # 等待2秒,确保新进程有足够的时间启动
if kill -0 $NEW_PID > /dev/null 2>&1; then
    echo "New process with PID $NEW_PID is running."
else
    echo "Failed to start the new process."
fi