Unix: How to View and Kill Processes Running on a Specific Port
Quick Summary: lsof -t -i :<port> | xargs kill -9
The Unix command lsof
("list open files") allows you to view
which process is listening on a specific port. This is useful when you cannot track down the process that's
using a port, such as a webserver listening on 8080.
The see which process(es) are running on a port:
sudo lsof -i :<port>
example: sudo lsof -i :8080
Explaination:
-i
Displays processes listening on an Internet address
Automatically kill the process listening on a port:
Pipe the process ID into the kill
command to kill it:
lsof -t -i :<port> | xargs kill -9
lsof -t -i :3000 | xargs kill -9
Explaination:
-t
: Terse output. Only displays the process ID numberxargs
: Takes the process ID from standard output and makes it the arguemnt to kill
kill -9
: Sends the SIGKILL signal to the process, causing it to terminate immediately