Today, I needed to find a way to stop a FastAPI server invoked by the uv package manager.

The process was started like this:

$ uv run uvicorn hello:app --host 127.0.0.1 --port 8888
INFO:     Started server process [60692]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8888 (Press CTRL+C to quit)

As this consists of two processes and I didn't have a process ID, I started with using ps to find the correct processes:

$ ps -ef | grep "uvicorn hello:app" | grep -v grep
  501 60691 58871   0 11:56AM ttys029    0:00.03 uv run uvicorn hello:app --host 127.0.0.1 --port 8888
  501 60692 60691   0 11:56AM ttys029    0:00.29 /opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/Resources/Python.app/Contents/MacOS/Python /Users/me/fastapi_demo/.venv/bin/uvicorn hello:app --host 127.0.0.1 --port 8888

Using the awk command, I was then able to filter out the process IDs:

$ ps -ef | grep "uvicorn hello:app" | grep -v grep | awk '{print $2}'
60691
60692

Then piping this to xargs and combining it with the kill command did the final trick:

ps -ef | grep "uvicorn hello:app" | grep -v grep | awk '{print $2}' | xargs kill