start_api.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # MinerU 业务 API 启动脚本
  3. # 1. 环境配置
  4. export MINERU_OPENAI_PORT=30000
  5. export MINERU_API_PORT=5282
  6. export MINERU_VL_SERVER="http://127.0.0.1:${MINERU_OPENAI_PORT}/v1"
  7. # 日志配置
  8. LOG_DIR="/var/log/minerU"
  9. LOG_FILE="${LOG_DIR}/mineru-api.log"
  10. echo "=========================================="
  11. echo "🚀 正在启动 MinerU 业务 API 服务..."
  12. echo "📍 端口: ${MINERU_API_PORT}"
  13. echo "🔗 后端: ${MINERU_VL_SERVER}"
  14. echo "📂 日志: ${LOG_FILE}"
  15. echo "=========================================="
  16. # 确保日志目录存在
  17. mkdir -p "${LOG_DIR}"
  18. # 2. 检查并清理端口占用
  19. python3 -c "
  20. import os, glob
  21. def cleanup(port):
  22. port_hex = f'{port:04X}'
  23. inodes = set()
  24. for net_file in ['/proc/net/tcp', '/proc/net/tcp6']:
  25. if os.path.exists(net_file):
  26. with open(net_file, 'r') as f:
  27. for line in f:
  28. parts = line.split()
  29. if len(parts) > 1 and parts[1].endswith(':' + port_hex): inodes.add(parts[9])
  30. if not inodes: return
  31. for fd_dir in glob.glob('/proc/[0-9]*/fd'):
  32. try:
  33. for fd in os.listdir(fd_dir):
  34. try:
  35. link = os.readlink(os.path.join(fd_dir, fd))
  36. for inode in inodes:
  37. if f'socket:[{inode}]' in link:
  38. pid = fd_dir.split('/')[2]
  39. if int(pid) != os.getpid():
  40. print(f' ⚠️ 清理占用端口 {port} 的进程 {pid}...')
  41. os.kill(int(pid), 9)
  42. except: continue
  43. except: continue
  44. cleanup(${MINERU_API_PORT})
  45. "
  46. # 3. 等待推理服务就绪
  47. echo "⏳ 正在等待推理服务 (端口 ${MINERU_OPENAI_PORT}) 就绪..."
  48. MAX_RETRIES=100
  49. COUNT=0
  50. 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
  51. sleep 3
  52. COUNT=$((COUNT + 1))
  53. if [ $COUNT -ge $MAX_RETRIES ]; then
  54. echo "❌ 错误: 推理服务超时未就绪,放弃启动 API。"
  55. exit 1
  56. fi
  57. echo " 等待中 ($((COUNT * 3))s)..."
  58. done
  59. echo "✅ 推理服务已就绪!"
  60. # 4. 启动服务 (使用 nohup 确保后台运行)
  61. echo "--- 启动于 $(date) ---" > "${LOG_FILE}"
  62. nohup mineru-api --host 0.0.0.0 --port ${MINERU_API_PORT} >> "${LOG_FILE}" 2>&1 &
  63. # 分离进程
  64. disown
  65. echo "=========================================="
  66. echo "✅ API 服务已在后台启动!"
  67. echo "📄 查看日志: tail -f ${LOG_FILE}"
  68. echo "=========================================="