Guide

Claude Code left dev servers running? Stop them

You ask an agent to fix a bug, it starts npm run dev to check, and when the session ends the server keeps going. A few sessions later ports 3000 to 3004 are taken and the fans are on. Here’s how to find what agents left behind and stop just that, without killing your own servers.

By Anmol Sethi · Updated

In short

List listening servers with lsof -nP -iTCP -sTCP:LISTEN, then check each one’s project folder and start time with lsof -a -p <PID> -d cwd and ps -o etime. Servers Claude Code started carry CLAUDECODE=1 in their environment, so you can list exactly those. Stop them with kill <PID>, and tell the agent to stop what it starts.

Ask AIChatGPTClaudePerplexityGoogle AIGrok

Why agents leave servers behind

  • They start the server in the background so they can keep working, then the session ends before they stop it.
  • Each attempt starts a new server, and Next.js or Vite moves to the next port instead of failing, so you get 3000, 3001, 3002.
  • Agents working in git worktrees delete the folder and keep the server. See dev servers in git worktrees.

List every server, with its project and age

for p in $(lsof -tiTCP -sTCP:LISTEN); do
  d=$(lsof -a -p $p -d cwd -Fn | grep '^n')
  echo "$p  $(ps -o etime= -p $p)  ${d#n}"
done
61504     02:41:10  /Users/you/code/shop
61877        14:02  /Users/you/code/shop-agent-fix

A server that has been up for hours in a folder you don’t remember is usually an agent’s.

Only the ones Claude Code started

Claude Code sets CLAUDECODE=1 for every command it runs, and child processes inherit it. So a server whose environment has it was started by Claude Code, even after the session ended:

for p in $(lsof -tiTCP -sTCP:LISTEN); do
  ps eww -p $p | grep -q CLAUDECODE=1 &&
    ps -o pid=,etime=,command= -p $p
done

Check the list, then stop them with kill <PID>.

Tell the agent to clean up

Put this in the project’s CLAUDE.md (Claude Code) or AGENTS.md (Codex, Cursor and others):

Before starting a dev server, check whether one is already running for this project and reuse it.
Use a fixed port and fail if it's taken (`--strictPort` for Vite).
Stop any server you started before you finish.

Agents follow it most of the time. Not always, so check now and then.

The one-click way: Portlivo

Portlivo lists Claude Code, Codex, Gemini CLI and other agents under Agents in the menu bar, with the servers each one started beneath it. A server whose Claude Code session has ended gets an orange warning, and several from one project fold into one row you can stop together. Choose the Agents filter and press Stop Visible to clear them all; your own servers stay running. More in AI agent servers.

Or let the agent do it: with Portlivo’s MCP server, Claude Code and Cursor can list your dev servers and stop the ones they started.

Questions people ask

Does Claude Code stop dev servers when the session ends?
Not always. A server started in the background can outlive the session. Check with lsof -nP -iTCP -sTCP:LISTEN and stop the leftovers.
How can I tell which servers an agent started?
Look at the project folder and start time of each server. For Claude Code, look for CLAUDECODE=1 in the process environment with ps eww -p <PID>.
Will stopping them break the agent?
Not after the session has ended. During a session, the agent may still be using its server; let it finish or ask it to stop the server itself.

Try Portlivo free for 7 days. Every feature, no account.Then $8.99 once, no subscription.

Download for Mac

Still have a question?

Write to us — a real developer reads every email and replies personally.

support@portlivo.com