In this short article, we would like to show how to kill process that listens on a specific port in Linux.
kill -9 $(lsof -t -i:8080 -sTCP:LISTEN)
8080 is a number of used port by a process that we want to kill.
How does it work?
lsofreturns details of opened files - in Linux common politics is to represent all things by files,
- we want to get only PID, so
-tparameter is useful to do it - returning only process ID (PID),
-i:8080we are able to find information about processes that use
-sTCP:LISTENwe will check only listening TCP sockets.
killprogram stops process on kernel level (because of
-9parameter) using PID from
More safe way example
Alternatively, for save we can do all steps manually to be sure that the proper program was killed.
- display processes that use port
lsof -w -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 16908 john 21u IPv4 220968 0t0 TCP *:8080 (LISTEN)
- kill the selected process (in out case it is PID
kill -9 16908