#!/bin/bash # 配置参数 GUNICORN_CMD="python3 /home/work/anaconda3/bin/gunicorn" APP_NAME="flask_wht_server_api:app" PORT="8216" WORKERS="6" LOG_FILE="log10bjh_wht_server_api_$(date +%y%m%d%H%M).log" PID_FILE="/tmp/gunicorn_wht_server_api.pid" # 与Gunicorn启动参数一致 TIMEOUT=30 # (1) 停止现有进程 stop_previous_process() { # 检查Gunicorn的PID文件 if [ -f "$PID_FILE" ]; then read -r PID < "$PID_FILE" echo "Found Gunicorn PID file with PID: $PID" if [ -n "$PID" ] && ps -p "$PID" > /dev/null; then echo "Stopping Gunicorn master process (PID: $PID)..." kill "$PID" sleep 2 if ps -p "$PID" > /dev/null; then echo "Force killing Gunicorn master..." kill -9 "$PID" fi fi rm -f "$PID_FILE" fi # 杀死所有残留的Worker进程 WORKER_PIDS=$(pgrep -f "flask_wht_server_api:app") if [ -n "$WORKER_PIDS" ]; then echo "Killing leftover worker processes: $WORKER_PIDS" kill -9 $WORKER_PIDS fi } # (2) 启动新进程 start_new_process() { echo "Starting new Gunicorn server..." CMD="$GUNICORN_CMD -w $WORKERS -b 0.0.0.0:$PORT --pid $PID_FILE $APP_NAME" #nohup $CMD >> "$LOG_FILE" 2>&1 & nohup $CMD > /dev/null 2>&1 & echo "Done. Server output is being written to $LOG_FILE" } # 执行 stop_previous_process sleep 2s start_new_process