The short answer
List what's listening with lsof -iTCP -sTCP:LISTEN -n -P and note the PID on the port you need (3000, 5173, 8000…). Before stopping it, check three things: the process (ps -o pid,ppid,user,etime,command -p and the PID), the project folder it runs in (lsof -a -d cwd -p and the PID), and that it belongs to you and isn't a service something else manages. Then stop that one PID with kill, and use kill -9 only if it's still running a few seconds later. A parent PID of 1 on its own doesn't prove a process is a leftover. Don't stop processes by name: pkill node stops every Node process, including your editor's.
On this page
Why agents leave processes running
Claude Code, Codex CLI, Gemini CLI, Cursor's agent, Aider, OpenCode and other agents run commands to check their work: npm run dev, vite, next dev, uvicorn --reload, a test watcher. Long-running ones go in the background so the agent can keep going. Most of the time they stop when the session does. They survive when:
- the session ended abruptly: the terminal was closed, the agent crashed or the Mac slept mid-session
- the command detached itself from the terminal, as
nohup,&withdisown, ordocker compose up -ddo - a server restarted itself in a child process that its parent didn't take with it
What you notice is “port 3000 is already in use” the next day, a fan running for no reason, or memory pressure that doesn't match what you have open.
Step 1: find what's listening
lsof -iTCP -sTCP:LISTEN -n -PCOMMAND PID USER FD TYPE NODE NAME
node 48211 you 23u IPv6 TCP *:3000 (LISTEN)
node 48377 you 19u IPv4 TCP 127.0.0.1:5173 (LISTEN)
Python 48502 you 6u IPv4 TCP 127.0.0.1:8000 (LISTEN)lsof -nP -iTCP:3000 -sTCP:LISTEN-n and -P show addresses and port numbers instead of names, which is faster and easier to read. The PID column is what you need next. Without sudo, lsof shows your own processes; that's the right scope, because those are the only ones you should be stopping.
Step 2: check the process, the project and the owner
A port number says nothing about whose server it is. Check three things before you stop anything.
The process
ps -o pid,ppid,user,etime,command -p 48211 PID PPID USER ELAPSED COMMAND
48211 1 you 21:14:07 node /Users/you/code/storefront/node_modules/.bin/next dev --turbopackThe project
lsof -a -p 48211 -d cwdThe command line and the working folder tell you which repository it belongs to. If it's a project you're still working on in another terminal or editor, leave it alone.
The owner, and whether something manages it
- USER should be you. Don't stop processes owned by root or another user to free a port; find out what they are first.
- Check for a manager. Servers started by
brew services, a launch agent, Docker Desktop or a process manager such as pm2 are meant to keep running, and the manager may start them again. Stop them through the manager instead:brew services stopand the service name,docker compose downin the project folder, or the tool's own stop command. - Look at the parent. The PPID column names the process that started it. If that's still a live terminal or agent session, the server isn't orphaned; stop it from there.
ps -axo pid,ppid,user,etime,command | awk -v me="$USER" '$2 == 1 && $3 == me' | grep -E 'node|vite|next|uvicorn|python|ruby|rails' | grep -v grepContainers are separate: docker ps lists the ones still running, and docker compose down in the project folder stops a project's set. Activity Monitor shows the same processes, but not their ports.
Step 3: stop that one process, politely first
kill 48211lsof -nP -iTCP:3000 -sTCP:LISTENkill sends SIGTERM, which lets the server close its connections and remove its own temporary files. Give it a few seconds and check again. Only if it's still there, use kill -9 with the same PID, which can't be ignored but skips that tidy-up.
Some dev servers run a parent process and one or more workers. If the port is still held after the parent stops, run the port check again and repeat steps 2 and 3 for the PID it shows now, rather than stopping everything that matches a name.
Making it happen less
- Ask the agent to stop the servers it started before it finishes, or to use a fixed port so a leftover is easy to spot.
- End sessions from the agent rather than by closing the terminal window.
- Prefer commands that run in the foreground of the agent's own shell over
nohupor detached containers.
The faster way: Agent sessions in iKnowMyMac
iKnowMyMac's Agent sessions section, under Projects, tracks each agent session from the moment the agent starts: the project folder, how long it ran, every process it started and the ports they hold, and what's still running after it ended. Because it saw the session start those processes, it doesn't have to guess from the parent PID. Prompts and transcripts are never read.
