run.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. PORT=${PORT:-8506}
  4. APP_MODULE=${APP_MODULE:-news_mcp.mcp_server_fastmcp:app}
  5. LOGFILE=${LOGFILE:-logs/server.log}
  6. PIDFILE=${PIDFILE:-logs/server.pid}
  7. mkdir -p "$(dirname "$LOGFILE")"
  8. mkdir -p "$(dirname "$PIDFILE")"
  9. if [ -f "$PIDFILE" ]; then
  10. PID="$(cat "$PIDFILE" 2>/dev/null || true)"
  11. if [ -n "$PID" ] && ps -p "$PID" > /dev/null 2>&1; then
  12. echo "Server already running (PID $PID)"
  13. exit 0
  14. fi
  15. rm -f "$PIDFILE"
  16. fi
  17. # If another process already binds the configured port, fail fast rather than
  18. # launching a second server and hiding the collision.
  19. if command -v curl >/dev/null 2>&1; then
  20. if curl -sf "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then
  21. echo "A server is already responding on port $PORT; refusing to start a second instance"
  22. exit 1
  23. fi
  24. fi
  25. UVICORN_BIN="${UVICORN_BIN:-}"
  26. if [ -z "$UVICORN_BIN" ]; then
  27. if [ -x ".venv/bin/uvicorn" ]; then
  28. UVICORN_BIN=".venv/bin/uvicorn"
  29. else
  30. UVICORN_BIN="uvicorn"
  31. fi
  32. fi
  33. export PYTHONPATH="$(pwd):${PYTHONPATH:-}"
  34. nohup "$UVICORN_BIN" "$APP_MODULE" --host 0.0.0.0 --port "$PORT" > "$LOGFILE" 2>&1 &
  35. echo $! > "$PIDFILE"
  36. echo "Uvicorn started on port $PORT (PID $(cat "$PIDFILE"))"