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

284 lines
7.6 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
# ============================================
# AI 文章生成服务启动脚本 - start_atlas_wen.sh
# 版本: 2.0 (支持虚拟环境)
# ============================================
# 配置参数(请根据实际环境修改)
PYTHON_ENV="/home/work/keyword_crawl/venv" # Python虚拟环境路径
APP_DIR="/home/work/ai_wht" # 应用代码目录
SCRIPT_NAME="generate_Atlas_Qianwen_article.py" # 主程序脚本
SCRIPT_PATH="$APP_DIR/$SCRIPT_NAME" # 完整脚本路径
LOG_DIR="$APP_DIR/logs" # 日志目录
LOG_FILE="$LOG_DIR/ai_article_service.log" # 日志文件
MAX_WAIT_TIME=10 # 最大等待时间(秒)
PROCESS_NAME="generate_Atlas_Qianwen_article.py" # 进程名
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查必要文件是否存在
check_prerequisites() {
print_info "检查环境配置..."
# 检查虚拟环境
if [ ! -d "$PYTHON_ENV" ]; then
print_warning "虚拟环境目录不存在: $PYTHON_ENV"
print_info "尝试使用系统Python..."
PYTHON_CMD="python3"
else
if [ -f "$PYTHON_ENV/bin/python" ]; then
PYTHON_CMD="$PYTHON_ENV/bin/python"
elif [ -f "$PYTHON_ENV/bin/python3" ]; then
PYTHON_CMD="$PYTHON_ENV/bin/python3"
else
print_error "虚拟环境中未找到Python可执行文件"
exit 1
fi
print_info "使用虚拟环境Python: $PYTHON_CMD"
fi
# 检查Python版本
PYTHON_VERSION=$($PYTHON_CMD --version 2>&1)
print_info "Python版本: $PYTHON_VERSION"
# 检查应用目录
if [ ! -d "$APP_DIR" ]; then
print_error "应用目录不存在: $APP_DIR"
exit 1
fi
# 检查脚本文件
if [ ! -f "$SCRIPT_PATH" ]; then
print_error "脚本文件不存在: $SCRIPT_PATH"
exit 1
fi
# 创建日志目录
if [ ! -d "$LOG_DIR" ]; then
print_info "创建日志目录: $LOG_DIR"
mkdir -p "$LOG_DIR"
if [ $? -ne 0 ]; then
print_warning "无法创建日志目录,将使用当前目录"
LOG_DIR="."
LOG_FILE="ai_article_service.log"
fi
fi
print_success "环境检查完成"
}
# 停止现有进程
stop_existing_process() {
print_info "检查运行中的进程..."
# 查找进程PID
PIDS=$(pgrep -f "$PROCESS_NAME" 2>/dev/null)
if [ -z "$PIDS" ]; then
print_info "没有找到运行中的进程"
return 0
fi
print_info "发现运行中的进程: $PIDS"
# 先尝试正常终止
for PID in $PIDS; do
print_info "停止进程 (PID: $PID)..."
kill $PID 2>/dev/null
done
# 等待进程终止
local wait_count=0
while [ $wait_count -lt $MAX_WAIT_TIME ] && pgrep -f "$PROCESS_NAME" > /dev/null; do
sleep 1
((wait_count++))
print_info "等待进程停止... ($wait_count/$MAX_WAIT_TIME 秒)"
done
# 如果进程还在运行,强制终止
if pgrep -f "$PROCESS_NAME" > /dev/null; then
print_warning "进程仍在运行,强制终止..."
pkill -9 -f "$PROCESS_NAME"
sleep 2
fi
# 最终检查
if pgrep -f "$PROCESS_NAME" > /dev/null; then
print_error "无法终止进程,请手动检查"
return 1
else
print_success "所有进程已停止"
return 0
fi
}
# 启动新进程
start_new_process() {
print_info "启动AI文章生成服务..."
# 切换到应用目录
cd "$APP_DIR" || {
print_error "无法切换到应用目录: $APP_DIR"
return 1
}
# 检查Python依赖
print_info "检查Python依赖..."
$PYTHON_CMD -c "import sys; print('Python路径:', sys.executable)" > /dev/null 2>&1
if [ $? -ne 0 ]; then
print_error "Python环境检查失败"
return 1
fi
# 启动服务
print_info "启动命令: $PYTHON_CMD $SCRIPT_NAME"
print_info "日志文件: $LOG_FILE"
# 使用nohup启动并重定向日志
nohup $PYTHON_CMD "$SCRIPT_NAME" >> "$LOG_FILE" 2>&1 &
local NEW_PID=$!
print_info "新进程PID: $NEW_PID"
# 等待进程启动
sleep 2
# 验证进程是否在运行
if ps -p $NEW_PID > /dev/null 2>&1; then
# 获取实际进程信息
ACTUAL_PID=$(pgrep -f "$PROCESS_NAME" | head -1)
# 检查进程是否健康运行
sleep 1
if ps -p $ACTUAL_PID > /dev/null 2>&1; then
print_success "AI文章生成服务启动成功!"
print_info "进程ID: $ACTUAL_PID"
print_info "日志文件: $LOG_FILE"
print_info "启动时间: $(date '+%Y-%m-%d %H:%M:%S')"
# 显示日志文件大小
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(wc -l < "$LOG_FILE")
print_info "日志行数: $LOG_SIZE"
fi
return 0
else
print_error "进程启动后异常退出"
# 检查日志获取错误信息
if [ -f "$LOG_FILE" ]; then
print_info "最后10行日志:"
tail -10 "$LOG_FILE"
fi
return 1
fi
else
print_error "进程启动失败"
# 检查日志获取错误信息
if [ -f "$LOG_FILE" ]; then
print_info "启动日志:"
tail -20 "$LOG_FILE"
fi
return 1
fi
}
# 显示状态
show_status() {
print_info "当前服务状态检查..."
PIDS=$(pgrep -f "$PROCESS_NAME" 2>/dev/null)
if [ -z "$PIDS" ]; then
print_warning "服务未运行"
else
print_success "服务正在运行"
for PID in $PIDS; do
# 获取进程详细信息
PS_INFO=$(ps -p $PID -o pid,user,etime,cmd --no-headers 2>/dev/null)
if [ ! -z "$PS_INFO" ]; then
print_info "进程详情: $PS_INFO"
fi
done
fi
# 显示日志文件信息
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(du -h "$LOG_FILE" | cut -f1)
LOG_LINES=$(wc -l < "$LOG_FILE")
LOG_MTIME=$(stat -c %y "$LOG_FILE" 2>/dev/null || stat -f %Sm "$LOG_FILE" 2>/dev/null)
print_info "日志文件: $LOG_FILE"
print_info "日志大小: $LOG_SIZE, 行数: $LOG_LINES"
print_info "最后修改: $LOG_MTIME"
fi
}
# 主函数
main() {
echo ""
echo "========================================="
echo " AI文章生成服务启动脚本 v2.0"
echo "========================================="
echo ""
# 显示配置信息
print_info "配置信息:"
echo " 虚拟环境: $PYTHON_ENV"
echo " 应用目录: $APP_DIR"
echo " 脚本文件: $SCRIPT_NAME"
echo " 日志文件: $LOG_FILE"
echo ""
# 检查前置条件
check_prerequisites
# 停止现有进程
stop_existing_process
if [ $? -ne 0 ]; then
print_error "停止现有进程失败,启动中止"
exit 1
fi
# 启动新进程
start_new_process
if [ $? -ne 0 ]; then
print_error "启动新进程失败"
exit 1
fi
# 等待片刻后显示状态
sleep 1
echo ""
show_status
echo ""
print_success "启动流程完成"
echo ""
}
# 执行主函数
main "$@"