ppstructure_v3_daemon.sh 6.1 KB

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