killserver.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. PIDFILE=${PIDFILE:-logs/server.pid}
  4. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  5. is_news_server() {
  6. local cmd="$1"
  7. case "$cmd" in
  8. *"news_mcp.mcp_server_fastmcp:app"*|*"run.sh"*|*"uvicorn"*)
  9. return 0
  10. ;;
  11. *)
  12. return 1
  13. ;;
  14. esac
  15. }
  16. stop_pid() {
  17. local pid="$1"
  18. if [ -n "$pid" ] && ps -p "$pid" > /dev/null 2>&1; then
  19. kill "$pid" 2>/dev/null || true
  20. sleep 1
  21. ps -p "$pid" > /dev/null 2>&1 && kill -9 "$pid" 2>/dev/null || true
  22. fi
  23. }
  24. if [ -f "$PIDFILE" ]; then
  25. PID=$(cat "$PIDFILE" 2>/dev/null || true)
  26. stop_pid "$PID"
  27. rm -f "$PIDFILE"
  28. fi
  29. # Sweep up stale server processes started from this project.
  30. while IFS= read -r line; do
  31. pid="${line%% *}"
  32. cmd="${line#* }"
  33. if is_news_server "$cmd"; then
  34. stop_pid "$pid"
  35. fi
  36. done < <(pgrep -af 'news_mcp\.mcp_server_fastmcp:app|uvicorn|run\.sh' || true)
  37. # Extra safety: if we were started from the project dir, kill any lingering
  38. # process that still has the app module in its command line.
  39. while IFS= read -r line; do
  40. pid="${line%% *}"
  41. cmd="${line#* }"
  42. if [[ "$cmd" == *"$SCRIPT_DIR"* ]] && [[ "$cmd" == *"news_mcp.mcp_server_fastmcp:app"* ]]; then
  43. stop_pid "$pid"
  44. fi
  45. done < <(pgrep -af 'news_mcp\.mcp_server_fastmcp:app' || true)