Skip to content
Black FridayBlack Friday: both apps $5.99, ends 1 Dec

Guide

How to kill leftover dev servers after Claude Code

Coding agents start dev servers to test their work. Usually those stop with the session. When they don't, they keep holding a port and memory until you find them. Here's how to find them and check them before you stop anything.

By · Updated · 7 min read

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, & with disown, or docker compose up -d do
  • 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

Every process listening on a TCP port
lsof -iTCP -sTCP:LISTEN -n -P
Example output (illustrative, shortened)
COMMAND   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)
Just one port
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

What it is, who owns it, who started it and how long it's been running
ps -o pid,ppid,user,etime,command -p 48211
Example output (illustrative)
  PID  PPID USER      ELAPSED COMMAND
48211     1 you    21:14:07 node /Users/you/code/storefront/node_modules/.bin/next dev --turbopack

The project

The folder the process is running in (the NAME column)
lsof -a -p 48211 -d cwd

The 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 stop and the service name, docker compose down in 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.
Your own dev processes whose parent is PID 1 (a starting list, not a kill list)
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 grep

Containers 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

Ask it to stop (SIGTERM)
kill 48211
Check it has gone
lsof -nP -iTCP:3000 -sTCP:LISTEN

kill 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 nohup or 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.

The Agent sessions section in iKnowMyMac: a Claude Code session that ended 22 minutes ago left next dev, vite and a node watcher running on ports 3000 and 5173, a live Codex CLI session, and a Cursor Agent session that left nothing running. Above the sessions, Agent Undo says 3 folders are protected and up to date, and each session's card ends with what it did to its folder, such as 14 changed, 3 deleted and 6 added, with Changes and Undo buttons.
Agent sessions in iKnowMyMac, rendered from the app with simulated sessions and figures.

FAQ

Questions

Something else? support@iknowmymac.com

How do I find what's using port 3000 on a Mac?

Run lsof -nP -iTCP:3000 -sTCP:LISTEN. The COMMAND and PID columns name the process; ps -o user,command -p and the PID shows its owner and full command line, which usually includes the project folder. For port 5000, it's usually AirPlay Receiver, not a dev server: see port 5000 or 3000 already in use.

Is it safe to kill a leftover dev server?

Yes, if you've checked it's yours, which project it belongs to, and that nothing else manages it. A dev server for a project you aren't working on holds no data you'd lose; you can start it again. Stop it with kill first so it can shut down cleanly, and use kill -9 only if it ignores that.

Does a parent PID of 1 mean a process is a leftover?

Not on its own. launchd, process 1, adopts processes whose parent has exited, which is what happens to a dev server when its terminal or agent goes away. But services launchd starts on purpose, and jobs you started with nohup, also have a parent PID of 1. Check the command, project folder and owner before stopping it.

Does this only happen with Claude Code?

No. Any agent or script that starts background processes can leave them behind, including Codex CLI, Gemini CLI, Cursor's agent, Aider and OpenCode, and plain terminal sessions too.

Know why your Mac is slow.

macOS 15 Sequoia or later · Apple silicon. A 55.3 MB native app, notarized by Apple. No account, no analytics, no telemetry.

One-time purchase · every feature · 5-day refund