#!/usr/bin/env bash set -euo pipefail PIDFILE=${PIDFILE:-logs/server.pid} SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" is_news_server() { local cmd="$1" case "$cmd" in *"news_mcp.mcp_server_fastmcp:app"*|*"run.sh"*|*"uvicorn"*) return 0 ;; *) return 1 ;; esac } stop_pid() { local pid="$1" if [ -n "$pid" ] && ps -p "$pid" > /dev/null 2>&1; then kill "$pid" 2>/dev/null || true sleep 1 ps -p "$pid" > /dev/null 2>&1 && kill -9 "$pid" 2>/dev/null || true fi } if [ -f "$PIDFILE" ]; then PID="$(cat "$PIDFILE" 2>/dev/null || true)" stop_pid "$PID" rm -f "$PIDFILE" fi # Sweep up stale server processes started from this project. while IFS= read -r line; do pid="${line%% *}" cmd="${line#* }" if is_news_server "$cmd"; then stop_pid "$pid" fi done < <(pgrep -af 'news_mcp\.mcp_server_fastmcp:app|uvicorn|run\.sh' || true) # Extra safety: if we were started from the project dir, kill any lingering # process that still has the app module in its command line. while IFS= read -r line; do pid="${line%% *}" cmd="${line#* }" if [[ "$cmd" == *"$SCRIPT_DIR"* ]] && [[ "$cmd" == *"news_mcp.mcp_server_fastmcp:app"* ]]; then stop_pid "$pid" fi done < <(pgrep -af 'news_mcp\.mcp_server_fastmcp:app' || true) # Remove stale pidfiles that point at dead processes if any remain. if [ -f "$PIDFILE" ]; then PID="$(cat "$PIDFILE" 2>/dev/null || true)" if [ -z "$PID" ] || ! ps -p "$PID" > /dev/null 2>&1; then rm -f "$PIDFILE" fi fi