stop.sh 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. # 定义默认启动模式及路径
  3. SERVER="all"
  4. WEB_CHAT_START_PAGE_JARS="./server"
  5. KILL_TIMEOUT=5 # 等待进程退出的最长时间(秒)
  6. cd "$WEB_CHAT_START_PAGE_JARS" || exit 1
  7. # 定义进程终止函数
  8. kill_old_process() {
  9. local jar_name=$1
  10. local pid_list=$(pgrep -f "java.*$jar_name")
  11. if [ -n "$pid_list" ]; then
  12. echo "Found existing process(es) for $jar_name: $pid_list"
  13. # 优雅终止
  14. kill $pid_list
  15. echo "Sent SIGTERM to process(es), waiting for termination..."
  16. # 等待进程退出
  17. local timeout=0
  18. while kill -0 $pid_list 2>/dev/null && [ $timeout -lt $KILL_TIMEOUT ]; do
  19. sleep 1
  20. ((timeout++))
  21. done
  22. # 强制终止未退出的进程
  23. if kill -0 $pid_list 2>/dev/null; then
  24. echo "Force killing process(es)..."
  25. kill -9 $pid_list
  26. fi
  27. # 二次确认
  28. if pgrep -f "java.*$jar_name" >/dev/null; then
  29. echo "Error: Failed to kill process for $jar_name!" >&2
  30. return 1
  31. fi
  32. fi
  33. return 0
  34. }