killserver.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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)
  46. # Remove stale pidfiles that point at dead processes if any remain.
  47. if [ -f "$PIDFILE" ]; then
  48. PID="$(cat "$PIDFILE" 2>/dev/null || true)"
  49. if [ -z "$PID" ] || ! ps -p "$PID" > /dev/null 2>&1; then
  50. rm -f "$PIDFILE"
  51. fi
  52. fi