# How to kill a process on a port on a Mac

To free a port on a Mac, find the process using it with lsof -i :3000 in Terminal, note the number in the PID column, and stop it with kill followed by that number. Replace 3000 with the port you need.

Source: https://activityplusmac.com/guides/kill-process-on-port-mac

## Find the process

Open Terminal and run lsof -i :3000. The output lists the command name and its PID. To show only processes that are listening, use lsof -nP -iTCP:3000 -sTCP:LISTEN.

## Stop it

1. Run kill followed by the PID. This asks the process to stop cleanly.
2. If it is still running after a few seconds, run kill -9 followed by the PID to end it at once.
3. Run the lsof command again to confirm the port is free.

## In one line

kill $(lsof -ti tcp:3000 -sTCP:LISTEN) finds and stops the program listening on port 3000. The LISTEN part matters: without it the command also stops anything connected to that port, such as a browser tab open on localhost:3000. Add -9 after kill only if the plain version does not work.

## Why port 5000 or 7000 is already in use

On macOS Monterey and later, AirPlay Receiver listens on ports 5000 and 7000. It shows up as Control Center in lsof. Turn it off in System Settings, under General, then AirDrop and Handoff, or use a different port for your server.

## With Activity+

The Developer tab in Activity+ lists every dev server and database with its port, the project folder it was started in, how long it has been running and how much memory it holds.

Each one has a Stop button. It also points out a server that has been left running for hours, and lists the ports that ordinary apps and macOS have open.

Docker containers are listed with their image, published ports, CPU and memory, each with a Stop button, and Homebrew services running in the background are listed too.

From Terminal, activityplus ports prints the same list, and with --json a script can read it.

## Questions

### How do I see what is running on a port on a Mac?

Run lsof -i followed by a colon and the port number, such as lsof -i :8080. It shows the command, its process ID and the user that owns it.

### What does "address already in use" mean?

Another process is already listening on that port. Find it with lsof and stop it, or start your server on a different port.

### Is it safe to use kill -9?

It works, but the process gets no chance to clean up. Try plain kill first and use -9 only when a process refuses to stop.
