51 lines
1.4 KiB
Bash
51 lines
1.4 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
# 配置参数
|
||
|
|
GUNICORN_CMD="python3 /home/work/anaconda3/bin/gunicorn"
|
||
|
|
APP_NAME="flask_article_server_search:app"
|
||
|
|
PORT="8321"
|
||
|
|
WORKERS="4"
|
||
|
|
LOG_FILE="log10bjh_article_search_$(date +%y%m%d%H%M).log"
|
||
|
|
PID_FILE="/tmp/gunicorn_article_search.pid" # 与Gunicorn启动参数一致
|
||
|
|
TIMEOUT=5
|
||
|
|
|
||
|
|
# (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_article_server_search: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 -t 30 --preload --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
|