File size: 2,199 Bytes
7c71fa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash

# 配置
APP_NAME="f5-tts-api"
PID_FILE="app.pid"
LOG_FILE="logs/startup.log"
PYTHON_CMD="uv run app.py"

# 获取当前脚本所在目录
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# 确保 logs 目录存在
mkdir -p logs

start() {
    if [ -f "$PID_FILE" ]; then
        pid=$(cat "$PID_FILE")
        if ps -p "$pid" > /dev/null; then
            echo "$APP_NAME is already running (PID: $pid)"
            return
        else
            echo "PID file exists but process is gone. Cleaning up."
            rm "$PID_FILE"
        fi
    fi

    echo "Starting $APP_NAME..."
    nohup $PYTHON_CMD > "$LOG_FILE" 2>&1 &
    pid=$!
    echo "$pid" > "$PID_FILE"
    echo "$APP_NAME started with PID $pid"
    echo "Logs are being written to $LOG_FILE"
}

stop() {
    if [ ! -f "$PID_FILE" ]; then
        echo "$APP_NAME is not running (PID file not found)"
        return
    fi

    pid=$(cat "$PID_FILE")
    if ps -p "$pid" > /dev/null; then
        echo "Stopping $APP_NAME (PID: $pid)..."
        kill "$pid"
        # 等待进程结束
        count=0
        while ps -p "$pid" > /dev/null; do
            sleep 1
            count=$((count + 1))
            if [ "$count" -ge 10 ]; then
                echo "Process did not stop after 10 seconds. Force killing..."
                kill -9 "$pid"
                break
            fi
        done
        rm "$PID_FILE"
        echo "$APP_NAME stopped"
    else
        echo "$APP_NAME is not running (Process not found)"
        rm "$PID_FILE"
    fi
}

restart() {
    stop
    sleep 2
    start
}

status() {
    if [ -f "$PID_FILE" ]; then
        pid=$(cat "$PID_FILE")
        if ps -p "$pid" > /dev/null; then
            echo "$APP_NAME is running (PID: $pid)"
        else
            echo "$APP_NAME is stopped (PID file exists but process is gone)"
        fi
    else
        echo "$APP_NAME is stopped"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0