ppstructure_v3_daemon.sh 6.0 KB

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