restart.sh 1.1 KB

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