killserver.sh 835 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. PORT="${MODEL_SELECTOR_PORT:-3300}"
  4. get_pids_with_lsof() {
  5. if command -v lsof >/dev/null 2>&1; then
  6. lsof -ti "tcp:${PORT}" 2>/dev/null || true
  7. fi
  8. }
  9. get_pids_with_fuser() {
  10. if command -v fuser >/dev/null 2>&1; then
  11. fuser "${PORT}/tcp" 2>/dev/null | tr ' ' '\n' | awk 'NF { print }' || true
  12. fi
  13. }
  14. pids="$(
  15. get_pids_with_lsof
  16. get_pids_with_fuser
  17. )"
  18. if [[ -z "${pids}" ]]; then
  19. exit 0
  20. fi
  21. printf '%s\n' "${pids}" | sort -u | while read -r pid; do
  22. [[ -z "${pid}" ]] && continue
  23. kill "${pid}" 2>/dev/null || true
  24. done
  25. sleep 1
  26. remaining="$(
  27. get_pids_with_lsof
  28. get_pids_with_fuser
  29. )"
  30. if [[ -n "${remaining}" ]]; then
  31. printf '%s\n' "${remaining}" | sort -u | while read -r pid; do
  32. [[ -z "${pid}" ]] && continue
  33. kill -9 "${pid}" 2>/dev/null || true
  34. done
  35. fi