ppstructure_v3_daemon.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/bin/bash
  2. # PaddleX PP-StructureV3 服务守护进程脚本
  3. LOGDIR="/home/ubuntu/zhch/logs"
  4. mkdir -p $LOGDIR
  5. PIDFILE="$LOGDIR/ppstructurev3.pid"
  6. LOGFILE="$LOGDIR/ppstructurev3.log"
  7. # 配置参数
  8. CONDA_ENV="paddle" # 根据您的study-notes.md中的环境名
  9. PORT="8111"
  10. CUDA_VISIBLE_DEVICES=7
  11. SCRIPT_DIR="/home/ubuntu/zhch/PaddleX/zhch"
  12. PIPELINE_CONFIG="$SCRIPT_DIR/my_config/PP-StructureV3.yaml"
  13. # 正确初始化和激活conda环境
  14. # 方法1:使用conda.sh脚本初始化
  15. if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then
  16. source /home/ubuntu/anaconda3/etc/profile.d/conda.sh
  17. conda activate $CONDA_ENV
  18. elif [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
  19. source /opt/conda/etc/profile.d/conda.sh
  20. conda activate $CONDA_ENV
  21. else
  22. # 方法2:直接使用conda可执行文件路径
  23. echo "Warning: Using direct conda path activation"
  24. export PATH="/home/ubuntu/anaconda3/envs/$CONDA_ENV/bin:$PATH"
  25. fi
  26. # 设置模型下载源(可选)
  27. export PADDLE_PDX_MODEL_SOURCE="bos"
  28. export PADDLEX_ENABLE_TABLE_ADAPTER="true"
  29. export PYTHONWARNINGS="ignore::UserWarning"
  30. start() {
  31. if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
  32. echo "PaddleX PP-StructureV3 is already running"
  33. return 1
  34. fi
  35. echo "Starting PaddleX PP-StructureV3 daemon..."
  36. echo "Port: $PORT, Device: $DEVICE"
  37. echo "Pipeline config: $PIPELINE_CONFIG"
  38. # 检查配置文件是否存在
  39. if [ ! -f "$PIPELINE_CONFIG" ]; then
  40. echo "❌ Pipeline config file not found: $PIPELINE_CONFIG"
  41. return 1
  42. fi
  43. # 检查conda环境
  44. if ! command -v python >/dev/null 2>&1; then
  45. echo "❌ Python not found. Check conda environment activation."
  46. return 1
  47. fi
  48. # 检查paddlex命令
  49. if ! command -v paddlex >/dev/null 2>&1; then
  50. echo "❌ PaddleX not found. Check installation and environment."
  51. return 1
  52. fi
  53. echo "🔧 Using Python: $(which python)"
  54. echo "🔧 Using PaddleX: $(which paddlex)"
  55. echo " CUDA Devices: $CUDA_VISIBLE_DEVICES"
  56. # 启动PaddleX服务
  57. cd $SCRIPT_DIR || exit 1
  58. nohup $CUDA_VISIBLE_DEVICES; python3 start_paddlex_with_adapter.py --serve \
  59. --port $PORT \
  60. --device "gpu" \
  61. --pipeline "$PIPELINE_CONFIG" \
  62. > $LOGFILE 2>&1 &
  63. echo $! > $PIDFILE
  64. echo "✅ PaddleX PP-StructureV3 started with PID: $(cat $PIDFILE)"
  65. echo "📋 Log file: $LOGFILE"
  66. echo "🌐 Service URL: http://localhost:$PORT"
  67. echo "📖 API Documentation: http://localhost:$PORT/docs"
  68. }
  69. stop() {
  70. if [ ! -f $PIDFILE ]; then
  71. echo "PaddleX PP-StructureV3 is not running"
  72. return 1
  73. fi
  74. PID=$(cat $PIDFILE)
  75. echo "Stopping PaddleX PP-StructureV3 (PID: $PID)..."
  76. # 优雅停止
  77. kill $PID
  78. # 等待进程结束
  79. for i in {1..10}; do
  80. if ! kill -0 $PID 2>/dev/null; then
  81. break
  82. fi
  83. echo "Waiting for process to stop... ($i/10)"
  84. sleep 1
  85. done
  86. # 如果进程仍在运行,强制结束
  87. if kill -0 $PID 2>/dev/null; then
  88. echo "Force killing process..."
  89. kill -9 $PID
  90. fi
  91. rm -f $PIDFILE
  92. echo "✅ PaddleX PP-StructureV3 stopped"
  93. }
  94. status() {
  95. if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
  96. PID=$(cat $PIDFILE)
  97. echo "✅ PaddleX PP-StructureV3 is running (PID: $PID)"
  98. echo "🌐 Service URL: http://localhost:$PORT"
  99. echo "📋 Log file: $LOGFILE"
  100. # 检查端口是否被监听
  101. if command -v netstat >/dev/null 2>&1; then
  102. if netstat -tuln | grep -q ":$PORT "; then
  103. echo "🔗 Port $PORT is being listened"
  104. else
  105. echo "⚠️ Port $PORT is not being listened (service may be starting up)"
  106. fi
  107. fi
  108. # 显示最新日志
  109. if [ -f $LOGFILE ]; then
  110. echo "📄 Latest logs (last 5 lines):"
  111. tail -5 $LOGFILE
  112. fi
  113. else
  114. echo "❌ PaddleX PP-StructureV3 is not running"
  115. if [ -f $PIDFILE ]; then
  116. echo "Removing stale PID file..."
  117. rm -f $PIDFILE
  118. fi
  119. fi
  120. }
  121. logs() {
  122. if [ -f $LOGFILE ]; then
  123. echo "📄 PaddleX PP-StructureV3 logs:"
  124. echo "=================="
  125. tail -f $LOGFILE
  126. else
  127. echo "❌ Log file not found: $LOGFILE"
  128. fi
  129. }
  130. config() {
  131. echo "📋 Current configuration:"
  132. echo " Conda Environment: $CONDA_ENV"
  133. echo " Port: $PORT"
  134. echo " Device: $DEVICE"
  135. echo " Pipeline Config: $PIPELINE_CONFIG"
  136. echo " Model Source: ${PADDLE_PDX_MODEL_SOURCE:-default}"
  137. echo " PID File: $PIDFILE"
  138. echo " Log File: $LOGFILE"
  139. if [ -f "$PIPELINE_CONFIG" ]; then
  140. echo "✅ Pipeline config file exists"
  141. else
  142. echo "❌ Pipeline config file not found"
  143. fi
  144. # 显示环境信息
  145. echo ""
  146. echo "🔧 Environment:"
  147. echo " Python: $(which python 2>/dev/null || echo 'Not found')"
  148. echo " PaddleX: $(which paddlex 2>/dev/null || echo 'Not found')"
  149. echo " Conda: $(which conda 2>/dev/null || echo 'Not found')"
  150. }
  151. # 显示使用帮助
  152. usage() {
  153. echo "PaddleX PP-StructureV3 Service Daemon"
  154. echo "======================================"
  155. echo "Usage: $0 {start|stop|restart|status|logs|config}"
  156. echo ""
  157. echo "Commands:"
  158. echo " start - Start the PaddleX service"
  159. echo " stop - Stop the PaddleX service"
  160. echo " restart - Restart the PaddleX service"
  161. echo " status - Show service status"
  162. echo " logs - Show service logs (follow mode)"
  163. echo " config - Show current configuration"
  164. echo ""
  165. echo "Configuration (edit script to modify):"
  166. echo " Port: $PORT"
  167. echo " Device: $DEVICE"
  168. echo " Pipeline: $PIPELINE_CONFIG"
  169. echo ""
  170. echo "Examples:"
  171. echo " ./ppstructure_v3_daemon.sh start"
  172. echo " ./ppstructure_v3_daemon.sh status"
  173. echo " ./ppstructure_v3_daemon.sh logs"
  174. }
  175. case "$1" in
  176. start)
  177. start
  178. ;;
  179. stop)
  180. stop
  181. ;;
  182. restart)
  183. stop
  184. sleep 3
  185. start
  186. ;;
  187. status)
  188. status
  189. ;;
  190. logs)
  191. logs
  192. ;;
  193. config)
  194. config
  195. ;;
  196. *)
  197. usage
  198. exit 1
  199. ;;
  200. esac