ppstructure_v3_daemon.sh 6.2 KB

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