stop.sh 808 B

123456789101112131415161718192021222324252627282930313233
  1. # 定义进程终止函数
  2. kill_old_process() {
  3. local jar_name=$1
  4. local pid_list=$(pgrep -f "java.*$jar_name")
  5. if [ -n "$pid_list" ]; then
  6. echo "Found existing process(es) for $jar_name: $pid_list"
  7. # 优雅终止
  8. kill $pid_list
  9. echo "Sent SIGTERM to process(es), waiting for termination..."
  10. # 等待进程退出
  11. local timeout=0
  12. while kill -0 $pid_list 2>/dev/null && [ $timeout -lt $KILL_TIMEOUT ]; do
  13. sleep 1
  14. ((timeout++))
  15. done
  16. # 强制终止未退出的进程
  17. if kill -0 $pid_list 2>/dev/null; then
  18. echo "Force killing process(es)..."
  19. kill -9 $pid_list
  20. fi
  21. # 二次确认
  22. if pgrep -f "java.*$jar_name" >/dev/null; then
  23. echo "Error: Failed to kill process for $jar_name!" >&2
  24. return 1
  25. fi
  26. fi
  27. return 0
  28. }