Find what's using a port on macOS, and which project it belongs to

By

Updated

lsof tells you a port belongs to node. Port Pilot's ports command adds the project folder, the framework, the process chain and the HTTP status, and finds the listeners its default list hides.

On my Mac, port 38586 is busy. lsof tells me who has it:

lsof -nP -iTCP:38586 -sTCP:LISTEN
COMMAND   PID        USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    69737 flaviocopes   28u  IPv6 0xcc289337b8a4ad2e      0t0  TCP [::1]:38586 (LISTEN)

It’s node. So is almost everything else I run. To know which project that is, I’d need ps for the full command, lsof again for its working directory, and a look at the package.json in there. (Find and kill the process using a port covers the plain lsof and kill route.)

Port Pilot does those lookups for you with the ports command. Here’s how to use it to go from a port number to a project, and what to do when the port doesn’t show up at all.

Install the command

After buying Port Pilot, download the source zip from your Polar receipt and unzip it. In that folder:

npm install
npm run verify
npm link

npm run verify type-checks the code, runs the tests and builds dist/. npm link puts ports on your PATH. You need Node.js 20 or newer. On macOS the scanner uses lsof, on Linux ss (or lsof when ss finds nothing).

Check one port

Pass the port to ports check:

ports check 38586
● Port 38586 is in use

  PID:       69737
  User:      flaviocopes
  Command:   astro dev --port 38586
  Project:   flaviocopes.com
  Framework: Astro
  Directory: /Users/flaviocopes/www/flaviocopes.com
  Chain:     ghostty → login -q -flp flaviocopes /bin/bash --noprofile --norc -c exec -l /opt/homebrew/bin/fish → fish → npm run dev  → node /opt/homebrew/bin/localhoster astro dev → astro dev --port 38586
  CPU:       146.4%
  Memory:    1.4 GB
  Uptime:    15h 42m

  Stop it with: ports kill 38586

That’s this site’s dev server, started 15 hours ago with npm run dev from a Ghostty tab. The chain also shows why it’s on such an odd port: localhoster started it. That’s Local Hoster, which gives each project a fixed port and a .localhost name (see give every project a stable .localhost URL).

There’s no HTTP line. The check requests / and gives up after 450 milliseconds, and this site’s home page takes about 6.5 seconds to render in dev. A faster server gets a status, a response time and the page title, like the fictional Next.js project in this screenshot:

ports check 3000 in Terminal showing a Next.js dev server called storefront, with its PID, command, project directory, the chain npm run dev to next dev, and HTTP 200 in 23ms with the page title

Each line comes from a different place:

FieldWhere Port Pilot gets it
PID and portlsof -iTCP -sTCP:LISTEN -P -n
Command, user, CPU, memory, uptimeps -axo pid,ppid,user,pcpu,rss,etime,command
ChainParent PIDs from the same ps table, up to 6 levels
DirectoryThe process’s working directory, from lsof -d cwd
ProjectThe name in the nearest package.json, searching up to 10 folders above the directory
FrameworkThe project’s dependencies: next, astro, vite and others, or Node.js when it has a main or start script
HTTP and titleA request to http://localhost:PORT/, then https://, with a 450 ms limit
Dockerdocker ps, when Docker answers within 900 ms

The project comes from the working directory, not from the command. Anything you start from inside a repo belongs to that repo. On my Mac a couple of coding agent processes listen on ports from the flaviocopes.com folder, and Port Pilot lists them as flaviocopes.com, Astro. Read the Command line before you trust the Project line.

Add --json to get the same record as data, which is what a script or an agent should read:

ports check 38586 --json

The object has free, port and an entry with pid, fullCommand, cwd, projectName, framework, processChain, http and docker. entry is null when the port is free.

Find the port when you only know the project

ports list scans every listener and keeps the ones that look like development work. --range narrows it down:

ports list --range 4000-5999
Port   Project             Framework   PID     User        Memory    HTTP       Command
────────────────────────────────────────────────────────────────────────────────────────────────────────
4327   —                   —           52027   flaviocopes 23 MB     404 13ms   astro.mjs preview --port 4327 --host 127.0.0.1 --json
4328   —                   —           72323   flaviocopes 29 MB     500 27ms   astro.mjs dev --port 4327 --host 127.0.0.1 --json
4399   flaviocopes.com     Astro       8294    flaviocopes 10.0 GB   —          astro dev --port 4399
5173   findabilly          Vite        4711    flaviocopes 23 MB     404 29ms   vite.js --host=127.0.0.1

4 listening ports

Look at 4328. That server was started with --port 4327, found 4327 taken by an astro preview, and moved one up. If you had a tab open on 4327, you’d be looking at the preview build, not the dev server.

The other filters work the same way:

ports list --project findabilly
ports list --framework vite
ports list --search astro

--project matches part of the project name, --framework the detected framework, and --search any field, including the command and the directory. --json prints the rows as an array.

For browsing, run ports with no arguments. The dashboard rescans every 3 seconds:

The Port Pilot dashboard in Terminal listing six fictional dev servers with their port, project, framework, PID, memory, uptime, HTTP status and command, and webhooks-worker on 8080 answering 503

Move with the arrow keys or j and k. The line under the table shows the selected row’s user and process chain. / searches, f cycles the filter between development services, all ports and Node only, and s cycles the sort between port, memory, uptime and name. e opens the project folder in Cursor, or VS Code if Cursor isn’t installed. d copies the directory.

When the port isn’t in the list

The default list keeps a listener only if it has a project, a known framework, a Docker mapping, or a command that mentions node, deno, bun, python, ruby, cargo or go run. Everything else is hidden. On my Mac that’s 12 listeners shown out of 32.

So ports list can be empty while the port is still taken. Port 7000 is a good example:

ports is-free 7000; echo $?
Port 7000 is occupied
1

ports check looks at every listener, so it finds it:

ports check 7000
● Port 7000 is in use

  PID:       735
  User:      flaviocopes
  Command:   ControlCenter
  Directory: /
  CPU:       0.0%
  Memory:    39 MB
  Uptime:    3d 2h
  HTTP:      403 http://localhost:7000/ (12ms)

  Stop it with: ports kill 7000

ControlCenter on 5000 and 7000 is macOS’s AirPlay Receiver. Turn it off in System Settings, or pick another port. Don’t kill it.

To see everything in one table, add --all:

ports list --all

In the dashboard, press f until the header says “all ports”. The menu bar app has a Show all toggle for the same thing.

How ports check finds the project behind a portPort Pilot lists listening sockets with lsof, looks up the process in ps, reads its working directory, walks up to the nearest package.json for the project name and framework, then probes the port over HTTP and asks Docker about published ports.lsof: PID listeningon the portps: command,parents, memorylsof: workingdirectoryNearestpackage.json:project andframeworkHTTP probe on / witha 450 ms limitdocker ps:container, ifpublished

Ports published by Docker

When a container publishes a port, the listener on your Mac is Docker’s own process, and the project folder tells you nothing. Port Pilot runs docker ps and matches the published port. ports list then shows docker: and the container name in the Command column, and ports check adds a Docker line with the container name, the image and the port inside the container.

Stop those with docker stop and the container name, not with a kill on the Mac-side process. If Docker isn’t running, or docker ps takes longer than 900 ms, the Docker details are missing and you only see Docker’s process. To match a port to its container by hand, see which Docker container published a local port.

Once you know what the process is and that you don’t need it, fix “address already in use” covers ports kill and what it does before it stops anything.

Get Port Pilot

Inspect local services, check their health, and stop the right process through a terminal dashboard, CLI, or macOS menu bar app.

See Port Pilot → · Read the docs · $19. See the app page for tax details, with full source code.

More tutorials for my apps →

Want me to talk about your product? You can sponsor this site.