Files
ai_wht_B/bigstart_api.sh
“shengyudong” 5a384b694e 2026-1-6
2026-01-06 14:18:39 +08:00

138 lines
3.9 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 配置参数
# 如果你使用的是虚拟环境建议直接使用虚拟环境的python
# 修改这里使用你的虚拟环境路径
VENV_PATH="/home/work/keyword_crawl/venv"
APP_PATH="/home/work/keyword_crawl" # 修改为你的应用所在目录
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"
TIMEOUT=30
# 进入应用目录
cd "$APP_PATH" || {
echo "Failed to change directory to $APP_PATH"
exit 1
}
# (1) 停止现有进程
stop_previous_process() {
echo "Checking for existing processes..."
# 检查Gunicorn的PID文件
if [ -f "$PID_FILE" ]; then
read -r PID < "$PID_FILE" 2>/dev/null
if [ -n "$PID" ]; then
echo "Found Gunicorn PID file with PID: $PID"
if ps -p "$PID" > /dev/null 2>&1; then
echo "Stopping Gunicorn master process (PID: $PID)..."
kill "$PID" 2>/dev/null
# 等待进程结束
local count=0
while ps -p "$PID" > /dev/null 2>&1 && [ $count -lt 10 ]; do
sleep 1
count=$((count + 1))
done
# 如果进程还在,强制杀死
if ps -p "$PID" > /dev/null 2>&1; then
echo "Force killing Gunicorn master..."
kill -9 "$PID" 2>/dev/null
fi
fi
fi
rm -f "$PID_FILE"
fi
# 杀死所有残留的Worker进程
WORKER_PIDS=$(pgrep -f "gunicorn.*$APP_NAME" 2>/dev/null || pgrep -f "flask_wht_server_api" 2>/dev/null)
if [ -n "$WORKER_PIDS" ]; then
echo "Killing leftover worker processes: $WORKER_PIDS"
kill -9 $WORKER_PIDS 2>/dev/null
sleep 2
fi
}
# (2) 启动新进程
start_new_process() {
echo "Starting new Gunicorn server..."
# 激活虚拟环境(如果需要)
if [ -f "$VENV_PATH/bin/activate" ]; then
source "$VENV_PATH/bin/activate"
fi
# 使用虚拟环境中的gunicorn
GUNICORN_CMD="$VENV_PATH/bin/gunicorn"
# 如果虚拟环境中没有gunicorn使用系统默认
if [ ! -f "$GUNICORN_CMD" ]; then
echo "Warning: gunicorn not found in virtual environment, using system default"
GUNICORN_CMD="gunicorn"
fi
# 构建命令
CMD="$GUNICORN_CMD -w $WORKERS -b 0.0.0.0:$PORT --pid $PID_FILE --timeout $TIMEOUT $APP_NAME"
echo "Executing: $CMD"
# 启动服务并记录日志
nohup $CMD >> "$LOG_FILE" 2>&1 &
# 保存后台进程PID
GUNICORN_PID=$!
echo "Gunicorn started with PID: $GUNICORN_PID"
# 等待一会儿确保服务启动
sleep 3
# 检查进程是否还在运行
if kill -0 $GUNICORN_PID 2>/dev/null; then
echo "Server started successfully."
echo "Log file: $LOG_FILE"
echo "PID file: $PID_FILE"
echo "Port: $PORT"
# 检查端口是否监听
if netstat -tulpn 2>/dev/null | grep ":$PORT " >/dev/null; then
echo "Port $PORT is listening."
else
echo "Warning: Port $PORT may not be listening yet."
fi
else
echo "Error: Server failed to start!"
echo "Check the log file for details: $LOG_FILE"
exit 1
fi
}
# 主执行流程
echo "========================================"
echo "Deploying Flask Application"
echo "========================================"
echo "Application: $APP_NAME"
echo "Port: $PORT"
echo "Workers: $WORKERS"
echo "Log file: $LOG_FILE"
echo "========================================"
# 停止旧进程
stop_previous_process
sleep 2
# 启动新进程
start_new_process
# 显示进程状态
echo ""
echo "Current process status:"
ps aux | grep -E "(gunicorn|$APP_NAME)" | grep -v grep
echo ""
echo "Deployment completed!"