#!/bin/bash # 配置参数 APP_DIR="/home/work/keyword_crawl" VENV_PATH="$APP_DIR/venv" # 使用绝对路径指定应用,或者使用相对路径 APP_NAME="flask_wht_server_api:app" # 原始方式 # 或者使用:APP_NAME="flask_wht_server_api" PORT="8216" WORKERS="1" # 先使用1个worker调试 LOG_FILE="$APP_DIR/log10bjh_wht_server_api_$(date +%y%m%d%H%M).log" PID_FILE="/tmp/gunicorn_wht_server_api.pid" TIMEOUT=120 # 进入应用目录 cd "$APP_DIR" || { echo "Failed to change directory to $APP_DIR" exit 1 } echo "当前目录: $(pwd)" echo "目录内容:" ls -la *.py # 激活虚拟环境 if [ -f "$VENV_PATH/bin/activate" ]; then source "$VENV_PATH/bin/activate" echo "✓ Virtual environment activated" echo "Python: $(which python)" else echo "Error: Virtual environment not found at $VENV_PATH" exit 1 fi # 添加当前目录到Python路径 export PYTHONPATH="$APP_DIR:$PYTHONPATH" echo "PYTHONPATH: $PYTHONPATH" echo "========================================" echo "Deploying Flask Application" echo "========================================" echo "Application: $APP_NAME" echo "Port: $PORT" echo "Workers: $WORKERS" echo "Log file: $LOG_FILE" echo "Directory: $(pwd)" echo "========================================" # 检查应用文件 APP_FILE="flask_wht_server_api.py" if [ ! -f "$APP_FILE" ]; then echo "Error: $APP_FILE not found!" echo "Files in directory:" ls -la exit 1 fi # 简单测试Python是否能导入模块 echo "Testing Python import..." python -c " import sys print('Python path:', sys.path) try: import flask_wht_server_api print('✓ Successfully imported flask_wht_server_api') except Exception as e: print(f'✗ Import failed: {e}') " # (1) 停止现有进程 echo "" echo "Stopping existing processes..." pkill -f "gunicorn.*flask_wht_server_api" 2>/dev/null pkill -f "python.*flask_wht_server_api" 2>/dev/null sleep 2 # 清除PID文件 [ -f "$PID_FILE" ] && rm -f "$PID_FILE" # (2) 启动新进程 - 使用不同的方式 echo "" echo "Starting Gunicorn server..." # 方法1:使用绝对路径 # CMD="gunicorn -w $WORKERS -b 0.0.0.0:$PORT --pid $PID_FILE --timeout $TIMEOUT --chdir $APP_DIR $APP_NAME" # 方法2:使用点号表示法(推荐) CMD="gunicorn -w $WORKERS -b 0.0.0.0:$PORT --pid $PID_FILE --timeout $TIMEOUT 'flask_wht_server_api:app'" echo "Executing: $CMD" # 直接运行(不后台运行,方便查看错误) echo "" echo "=== 直接运行(查看实时输出)===" gunicorn -w $WORKERS -b 0.0.0.0:$PORT --pid $PID_FILE --timeout $TIMEOUT --access-logfile - --error-logfile - 'flask_wht_server_api:app'