Commands
Every command, flag, and exit code — the whole tool on one page.
ports · ports list
Display every listening TCP port with its framework, project directory, and uptime. Supports --json output.
| Column | Value |
|---|---|
| PORT | The TCP port number. |
| PROCESS | The human-readable name of the service — framework detection first, then container image names, then raw process names. |
| PROJECT | For a process, its project directory's name, with just enough of its parents to tell two same-named projects apart (work/api, oss/api) — --json and doctor's header carry the full path. Otherwise a docker: container reference, a systemd: unit, windows, user: <name> for another user's listener ports couldn't trace, or — when unknown. |
| UPTIME | Duration with s, m, h, d suffixes — or ? when unknown. |
| URL | Where a web dev server answers — http://shop.localhost while the router runs, else its own address, http://localhost:3000 — for the frameworks that serve plain HTTP unless told otherwise (Next.js, Vite, Astro, Remix, Nuxt, SvelteKit, Storybook, FastAPI, Django, Flask, Rails, Laravel, Phoenix, Spring Boot, Gin, and their kin). One URL per app, on its own port: the one its command line names with --port/-p, else its lowest port that isn't a debugger's (--inspect, JDWP, debugpy, LiveReload). HTTPS chosen on the command line (--ssl, --cert, --experimental-https…) gets no http:// URL; HTTPS set in a config file can't be seen. Known from the framework, never by sending a request, so listing stays side-effect free; the column appears only when a row has one, and is a clickable link in a terminal that supports it (plain text in a pipe or with NO_COLOR). ports doctor verifies a port answers HTTP when you ask about it. |
ports find <name>
Filter the table. Case-insensitive, matches substrings across name, framework, process, project, container, and systemd unit names — framework names in their typed form too, so ports find nextjs finds Next.js. The listing filters (--port, --framework, --project, --uptime) narrow the matches further.
$ ports find postgres
5432 PostgreSQL docker: shop-db 2dports here [dir]
What's running for this project — the directory you're in, or the one named. Ask before starting a dev server, and reuse the one that's already up instead of starting a second on the next free port.
$ cd ~/projects/shop/web && ports here
PORT PROCESS PROJECT UPTIME URL
3000 Next.js shop 14m http://shop.localhost
8000 FastAPI shop 14m http://api.shop.localhost
5432 PostgreSQL docker: shop-db 2dThe project is found the way the PROJECT column finds it — the outermost .git, then workspace markers, then the nearest package.json, go.mod, and friends — so any subdirectory works. Compose containers belong when docker compose up ran inside the project. The listing filters and formats apply.
Exits 0 when something is running and 1 when nothing is, so ports here --port 3000 || npm run dev starts a server only when none is up. With --json, nothing running prints [] — never empty output.
ports wait <port|name>
Block until something listens, then print its row — the step between starting a server and using it, without a sleep-and-retry loop.
$ npm run dev &
$ ports wait 3000 --http
PORT PROCESS PROJECT UPTIME URL
3000 Next.js shop 3s http://shop.localhost
$ ports stop 3000 && ports wait 3000 --gone
✓ killed Next.js (pid 48391) — port 3000 is free
✓ :3000 is freeA port (3000 or :3000) waits for any listener on it — anything numeric is read as a port, so a service named 2048 is waited on by its port, not its name; a name waits for a service ports stop would match — a name like api.shop, a project, framework, container, or process. --http waits until it answers an HTTP request, on 127.0.0.1 or ::1: dev servers often bind before they can serve. With a name, --http waits for the one server that name reaches. --gone waits until the port or service is free. --timeout gives up after 30s by default (0 waits forever).
Exits 0 when the condition is met, 1 when the timeout passes first — saying what it was waiting on and the command that moves things along — 2 for a bad invocation, and 130 when interrupted. ports wait only watches; it never starts anything.
ports open <port|name>
Open what a listener serves in your default browser.
$ ports open 3000
→ opening http://localhost:3000/
$ ports open shop
open which? ↑/↓ to move, enter to choose, esc to leave
PORT PROCESS PROJECT UPTIME URL
› 3000 Next.js shop 2h http://localhost:3000
6006 Storybook shop 2h http://localhost:6006A port opens that port; a name opens the web server ports stop would match. A name the router serves opens as that name, http://shop.localhost. Otherwise ports asks the listener before it opens anything — one HEAD over HTTPS, then over HTTP — so the browser gets the scheme the server really speaks, and a port that isn't a web server (a database, a debugger, one still starting) is reported with exit 1 rather than opened. A cold dev server gets a few seconds to compile its first page before it answers; a plain-HTTP server may log the HTTPS attempt as a bad request. A name with several web servers behind it opens a picker in a terminal; off one — a script, an agent — it's a usage error (exit 2) that lists the ports, so name one.
$BROWSER is honoured (its first entry, split on spaces, no shell; %s stands for the URL). Otherwise ports uses open on macOS, xdg-open on Linux, wslview or explorer.exe inside WSL, and the URL handler on Windows. If no browser starts, the URL is printed and the exit is 1. ports opens a browser, never a server.
ports stop <port|name>
Stop what you name. A port (3000 or :3000) stops whatever listens on it; anything else is a name — a name like api.shop, a project, framework, container, systemd unit, or process — and stops every service it matches. Anything all-digits is read as a port, so a project named 2048 stops by name with ports kill 2048.
$ ports stop 3000
✓ killed Next.js (pid 48391) — port 3000 is free
$ ports stop shop
✓ stopped Next.js (:3000)
✓ stopped FastAPI (:8000)
✓ stopped PostgreSQL (:5432)
$ ports stop api.shop
✓ stopped FastAPI (:8000)SIGTERM first, SIGKILL only if the process doesn't exit; --force skips straight to SIGKILL. Docker-backed ports stop the container through the engine — never docker-proxy.
Run bare in a terminal, ports stop opens a picker: the listing's rows, ↑/↓ (or k/j) to move, Enter to choose, y to confirm. The confirm names every port the process holds, and only that process is stopped — if it exited, restarted or gave up the port while you were choosing, nothing is stopped and you're told to run ports stop again. --dry-run and --force apply as usual. Rows only sudo can stop are dimmed and skipped. Esc, q or Ctrl-C leave with nothing stopped (exit 130). Scripts, pipes and agents never see the picker: off a terminal, bare ports stop is a usage error (exit 2), so a forgotten argument fails loudly instead of waiting on a prompt.
ports free <port> and ports kill <name> — the port-only and name-only commands from v0.1.0 — still work exactly as before; stop is the one to reach for.
Guarded stops: --expect
stop stops whatever matches — --expect <name> makes it check first. Every target it would stop has to match the name the way a name stop matches one (project, framework, container, systemd unit, or process); if any doesn't, nothing is stopped and ports says what is actually there:
$ ports stop 3000 --expect nextjs
✗ :3000 is PostgreSQL (docker: shop-db), not "nextjs"
→ nothing was stopped — drop --expect to stop regardless, or `ports doctor 3000` to look closerIt's the guard to put on any stop a script or an agent runs: "stop :3000" becomes "stop :3000 if it's still my dev server". A listener whose owner is hidden can't prove what it is, so the guard refuses it. With PORTS_REQUIRE_EXPECT=1 set, every stop, free and kill must carry --expect — one that doesn't is refused with exit 2 — except a dry run, and the picker.
Results as JSON
With --json, stop reports one object per target — a process, container, or service, however many ports it held:
[
{
"action": "stopped",
"ports": [3000, 3001],
"service": { "port": 3000, "pid": 48391, "framework": "Next.js", "project": "/home/dev/projects/shop", … }
},
{
"action": "failed",
"ports": [3306],
"service": { "port": 3306, "pid": 0, "framework": "MySQL", "unit": "mysql", … },
"error": "a systemd service running as mysql — stop it with `sudo systemctl stop mysql`",
"fix": "sudo systemctl stop mysql"
}
]action is stopped, would_stop (under --dry-run), failed, refused (by --expect), or skipped; service is the row exactly as ports --json prints it; fix is the command that moves a failure along. Nothing to stop prints [] and exits 1.
A name can sweep up another user's listener — one the scan sees with no pid, which nothing short of sudo can stop. Those are skipped, each with the command that would stop it (sudo systemctl stop <unit> or sudo ports stop <port>). A skipped row never fails a stop that did what it could, and --expect judges only what would actually be stopped; when skipped rows are all there was, nothing was stopped and it exits 1. A port names its listener outright, so a port held that way is a failure, not a skip.
ports doctor [port]
Explain what a port's row means, and why a stop failed when one did. Doctor never acts — every finding is an observation, every fix is a command for you to run.
$ ports stop 3000
✗ Next.js (:3000): pid 48391 survived SIGKILL
→ run `ports doctor 3000` to see why
$ ports doctor 3000
:3000 Next.js pid 48391 ~/projects/shop up 14m
✗ pid 48391 is in uninterruptible sleep (state D) — signals, SIGKILL included, are held until its I/O returns
→ wait out the I/O, or reboot — this process cannot be killed
✓ pid 48391 still matches the scan — no pid reuse
✓ nothing else holds :3000 — no stray socketsWhat it checks:
| Finding | What it means |
|---|---|
listener | Whether anything is listening — and, when nothing is, what holds the port instead (TIME_WAIT sockets, another user's process). |
http | When the listener answers an HTTP request, the URL it verified: ✓ responds to HTTP → http://localhost:3000. A port that doesn't speak HTTP — postgres, redis — gets no finding. |
multiplicity | More than one process on the port (SO_REUSEPORT). ports stop <port> stops all of them. |
state | The kernel's process state — uninterruptible sleep (state D, why SIGKILL didn't land), zombie (the port is effectively free), suspended. |
pid-freshness | Whether the pid still belongs to the process the scan saw — a stale row means the pid was reused. |
container | A docker-proxy with no container behind it, and whether the engine is reachable to ask. |
sockets | Everything else on the port: TIME_WAIT (ages out in ~60s) and CLOSE_WAIT (a leak hint in the app) as · info, live client connections. |
windows / mirror | On WSL2: a listener on the Windows side signals can't reach, and the same port held on both sides. |
Bare ports doctor runs the machine-wide subset — the container engine, every proxy row that didn't resolve, every port shared by more than one process, and every listener a plain scan can't attribute. Once dev servers have names it adds the router: whether one runs and on which port, and whether this machine's own resolver finds each name the way Node, Python and Go would. Those are info, so a machine that doesn't use names never fails doctor over them.
Findings marked · are info: something worth knowing with nothing to do about it, like TIME_WAIT sockets on a healthy server. They never fail the exit code.
Exit codes are grep-style: 0 when nothing needs doing (every finding is ✓ or ·), 1 when anything is ⚠ or ✗ — so ports doctor 3000 && ./deploy.sh gates naturally — and 2 for bad arguments. --format json emits the findings as an array of {check, status, message, fix} objects, where status is ok, info, warn, or fail.
ports url [name]
The URL a dev server answers at, by its name — the one to hand a script or a test runner instead of a port.
$ ports url api.shop
http://api.shop.localhost
$ curl "$(ports url api.shop)/health"With no name it's the app in the directory you're in, else the project's main app. While the router runs the URL is the name's; without one it's the server's own address, http://localhost:8000, so a script works either way. --local always prints the server's own address — for clients that can't resolve *.localhost. --json prints {"name", "url", "port", "local_url"}. Exits 1 when nothing running has that name, listing the names that are.
ports run [name] -- <command>
Start a dev server on a free port, under its name, for as long as it runs.
$ ports run -- npm run dev
→ http://shop.localhost (:4123)The port comes from 4000–4999 — the same one each time the name runs, when it's free — or --port, or the one the command names itself (--port 3000, -p 3000). It reaches the server the way its framework takes one: PORT (and FLASK_RUN_PORT, UVICORN_PORT) for Next.js, Nuxt and most servers; --port with --strictPort for Vite; --port for Astro and Angular — after the -- that npm run needs. The framework is read from the package.json script behind npm run dev, pnpm dev, yarn dev or bun run dev.
The name is the project's, as ports names any server; give one before the -- to choose. When the name is already taken — the project running elsewhere, an alias, another ports run — it says where instead of starting a second copy, and exits 1.
ports run starts only the command given and supervises nothing. Signals reach the command and everything it started — npm alone would leave Vite holding the port — the name goes when the command exits, and ports run exits with the command's code (127 when the command doesn't exist).
ports alias [name] [port]
A fixed name for a port ports can't name on its own — a server outside any project, or one it doesn't recognise.
$ ports alias stripe 4242
✓ stripe.localhost → :4242
$ ports alias # list them
$ ports alias stripe --remove
✓ removed stripe.localhostAn alias wins over any other name for its port, routes whatever listens there, and lasts until removed — across restarts and reboots. When nothing holds the port, the name says so. --json lists [{"name", "port"}].
ports proxy
The router: every web dev server at http://<name>.localhost.
| Command | Does |
|---|---|
ports proxy | Runs it in the foreground, printing each name and every change, until Ctrl-C. --port picks the port, with no fallback. |
ports proxy start | Runs it in the background, started again at login — a systemd user unit on Linux, a launchd agent on macOS, a background process elsewhere. On Linux, in a terminal, offers the one-time setcap for port 80. Exits 0 when it already runs. |
ports proxy status | Whether it runs, on which port, and how it's kept running. Exits 0 when it runs, 1 when it doesn't. --json prints {"running", "pid", "port", "managed_by", "starts_at_login", "log"}. |
ports proxy stop | Stops it — a background one, or one in another terminal — and removes what start set up. Dev servers keep running. |
ports hosts
Names for the clients that don't resolve *.localhost on their own — Node's fetch, Python and Go on Windows and some Linux setups. See names for every client.
| Command | Does |
|---|---|
ports hosts | What the hosts file's ports block holds, and which running names it lacks. --json prints {"file", "names", "missing"}. |
ports hosts sync | Writes the running names and aliases into one marked block, through sudo. Keeps names already there unless --prune. |
ports hosts clean | Takes the block out, leaving the file as it was. |
ports mcp
A Model Context Protocol server on stdin and stdout, for coding agents: list, here, find, url, wait, stop (with expect required) and doctor, each the command run with --json. It starts nothing. See Agents.
claude mcp add ports -- ports mcpFlags
Each command takes the flags it uses and no others. A flag given to a command that wouldn't use it is an error (exit 2) that names the commands it belongs to — never silently ignored.
| Flag | Commands | Effect |
|---|---|---|
--format table|json | the listing commands, stop, wait, doctor, url | Output format. json is an array of {port, pid, process, framework, project, url, started, container, user, unit, …} objects — the seam for scripts and agents. |
--json | every command that prints a result | Short for --format json. stop reports result objects. |
--port | ports, find, here | Filter by port(s): 3000, 3000-4000, :3000,:8000. |
--framework | ports, find, here | Filter by framework(s) as you'd type them — case, spaces, and punctuation don't matter: nextjs,vite, springboot. |
--project | ports, find, here | Filter by project name or path: shop, ~/projects/shop. |
--uptime | ports, find, here | Filter by uptime: >1h, <30m, 2h. |
--sort port|uptime|process|project | ports, find, here | Display order — longest uptime first, say, or plain port order. The default groups rows by project so one project's services read as one block; an explicit --sort replaces that arrangement. |
--watch / -w | ports | Live refresh: rescan and redraw until you interrupt it. Table mode clears and redraws the terminal; JSON mode emits one array per tick. |
--interval | ports | Rescan interval for --watch (default 1s, minimum 250ms). Watch never stacks scans — the next one starts only after the last one finishes. |
--force | stop | Skip SIGTERM and kill immediately. |
--dry-run | stop | Show what would be stopped, without stopping it. |
--expect | stop | Stop only if every target it would stop matches this name — otherwise stop nothing; skipped rows aren't judged. See guarded stops. |
--timeout | wait | Give up after this long (default 30s; 0 waits forever). |
--http | wait | Wait until it answers HTTP, not just accepts connections. With a name, the one server the name reaches. |
--gone | wait | Wait until the port or service is free instead. |
--local | url | Print the server's own address, http://localhost:4123, not the name's. |
--port | run, proxy | Run the command on this port; run the router on this port, with no fallback. |
--remove | alias | Drop the alias. |
--prune | hosts sync | Drop names from the hosts file that no server or alias holds now. |
When a filter leaves the table empty, ports says nothing listening matches the filter rather than claim nothing is listening.
On a terminal, output is lightly coloured: uptime and other users' rows are dimmed, and ✓, ⚠ and ✗ are green, yellow and red. Each stream decides for itself — a piped stdout is plain even while stderr is still the terminal. NO_COLOR (set and not empty), TERM=dumb, a pipe or a file turn colour off, and the bytes are then exactly what they'd be without it. --json never carries colour. On Windows, colour needs Windows Terminal or another terminal that sets TERM_PROGRAM; the legacy console gets plain text.
Config
None is required — every row is inferred from the machine itself, and the tool is fully useful sixty seconds after install with no file anywhere. But ports-config.yaml is an opt-in extension layer for the two things a scan can't guess: your own framework names and your own project markers. It's looked up in ., ~/.config/ports, and /etc/ports:
# frameworks you run that the built-in table doesn't know — same rules
# the detector uses: exe prefixes, exact bin names, cmdline words
# (whole words; fragments with / @ . or spaces match as substrings)
frameworks:
- name: Vend
exe: ["vend"]
- name: Internal CLI
bin: ["devcli"]
cmd: ["internal-cli"]
# extra marker files that make a directory read as a project root
projects:
markers: [".vendroot", "vendor.json"]A custom framework rule wins over the built-in table; custom markers join the built-in set (.git, go.mod, package.json, Cargo.toml, and friends). If the file exists but doesn't parse — or a framework rule has no name, or no exe, bin, or cmd and so would match every process — ports names the file and the problem and exits 2 rather than guessing. Nothing else reads config — flags, filters, and commands are set on the command line, always.
Exit codes
The same three codes everywhere, grep-style — so ports doctor 3000 && ./deploy.sh gates naturally and ports find redis || echo "not running" reads like the grep you already know.
| Code | Meaning |
|---|---|
0 | Clean — the table rendered, every stop succeeded, every doctor finding was ✓. |
1 | The invocation was fine, but there's something to report: find matched nothing, here found nothing running, wait timed out, open found nothing answering HTTP or couldn't start a browser, a stop failed or --expect refused it, stop found only rows it had to skip, or doctor found something ⚠ or ✗. |
2 | The invocation itself was unusable — an unknown command or flag, a flag on a command that doesn't take it, a bad value (--sort name, --format yaml, a port outside 1–65535 like 70000 — never read as a name), a wrong argument count, a config file that exists but doesn't parse, or open given a name with several web servers and no terminal to ask on. |
An interrupted ports wait exits 130 — its condition was never met — and so does a stop or open picker left without choosing; an interrupted ports --watch exits 0, since Ctrl-C is how a watch ends. Errors report on stderr; stdout stays clean for whatever's piping it — and with --json, a query that found nothing still prints [].
Environment
| Variable | Effect |
|---|---|
PORTS_REQUIRE_EXPECT | Set (and not 0, false or no), every stop, free and kill must carry --expect. |
PORTS_STATE_DIR | Where ports keeps names, aliases and the router's whereabouts. See where it keeps state. |
NO_COLOR | Plain output, no colour or links. |
BROWSER | The browser ports open starts. |
Scripts and agents
Every command speaks JSON, reports errors on stderr and exits grep-style, so a script or a coding agent can drive ports without parsing tables. The loop, the MCP server and the contracts are on Agents.
Permissions
Without root, the OS hides which process holds another user's socket — but not who owns it. On Linux, ports reads each socket's owner and the processes running as that user, and names the row when the evidence leaves one answer: the owner runs a single systemd service (mysql → mysql.service), or exactly one server known for that port (root's :80 → apache2.service). Those rows read systemd: <unit> in the PROJECT column, carry user and unit in JSON, and keep pid: 0 — ports knows the service, not which of its processes holds the socket. Stopping one points you at sudo systemctl stop <unit>.
When the evidence is ambiguous, the row stays ? with its owner (user: root) rather than a guess. Run sudo ports to reveal and stop those.
On WSL2, Windows-side sockets that Windows itself reports with no owner (pid 0, shown by netstat as System Idle Process) are left out — nothing can name or stop them.