#!/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