Every so often I get stuck with a running process that’s using a specific port, preventing me from running some new application that uses the same port. Then I’m left Googling for solutions or rebooting the machine to make progress. But not any more! I’m recording the solution for my future self (and of course for you, dear reader).
> sudo lsof -i :<PortNumber> # returns list of processes using the port, with PID
> kill -9 <PID> # kill the specific pid
For example, you can find out what’s running on port number 8080
by
running the command:
❯ sudo lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 50693 kevinsookocheff 369u IPv6 0x861ee023f0aa0b29 0t0 TCP *:http-alt (LISTEN)
lsof
returns the PID of the running process that uses the suspect port.
You can terminate that process with the kill
command:
❯ kill -9 50693
And voilà! The port is free once more!