#!/bin/bash # MinerU 业务 API 启动脚本 # 1. 环境配置 export MINERU_OPENAI_PORT=30000 export MINERU_API_PORT=5282 export MINERU_VL_SERVER="http://127.0.0.1:${MINERU_OPENAI_PORT}/v1" # 日志配置 LOG_DIR="/var/log/minerU" LOG_FILE="${LOG_DIR}/mineru-api.log" echo "==========================================" echo "🚀 正在启动 MinerU 业务 API 服务..." echo "📍 端口: ${MINERU_API_PORT}" echo "🔗 后端: ${MINERU_VL_SERVER}" echo "📂 日志: ${LOG_FILE}" echo "==========================================" # 确保日志目录存在 mkdir -p "${LOG_DIR}" # 2. 检查并清理端口占用 python3 -c " import os, glob def cleanup(port): port_hex = f'{port:04X}' inodes = set() for net_file in ['/proc/net/tcp', '/proc/net/tcp6']: if os.path.exists(net_file): with open(net_file, 'r') as f: for line in f: parts = line.split() if len(parts) > 1 and parts[1].endswith(':' + port_hex): inodes.add(parts[9]) if not inodes: return for fd_dir in glob.glob('/proc/[0-9]*/fd'): try: for fd in os.listdir(fd_dir): try: link = os.readlink(os.path.join(fd_dir, fd)) for inode in inodes: if f'socket:[{inode}]' in link: pid = fd_dir.split('/')[2] if int(pid) != os.getpid(): print(f' ⚠️ 清理占用端口 {port} 的进程 {pid}...') os.kill(int(pid), 9) except: continue except: continue cleanup(${MINERU_API_PORT}) " # 3. 等待推理服务就绪 echo "⏳ 正在等待推理服务 (端口 ${MINERU_OPENAI_PORT}) 就绪..." MAX_RETRIES=100 COUNT=0 while ! python3 -c "import socket; s=socket.socket(); s.settimeout(1); exit(s.connect_ex(('127.0.0.1', ${MINERU_OPENAI_PORT})))" 2>/dev/null; do sleep 3 COUNT=$((COUNT + 1)) if [ $COUNT -ge $MAX_RETRIES ]; then echo "❌ 错误: 推理服务超时未就绪,放弃启动 API。" exit 1 fi echo " 等待中 ($((COUNT * 3))s)..." done echo "✅ 推理服务已就绪!" # 4. 启动服务 (使用 nohup 确保后台运行) echo "--- 启动于 $(date) ---" > "${LOG_FILE}" nohup mineru-api --host 0.0.0.0 --port ${MINERU_API_PORT} >> "${LOG_FILE}" 2>&1 & # 分离进程 disown echo "==========================================" echo "✅ API 服务已在后台启动!" echo "📄 查看日志: tail -f ${LOG_FILE}" echo "=========================================="