85

Find processes based on port number and kill them all.

ps -efl | grep PORT_NUMBER | kill -9 process_found_previously

how to complete the last column?

Oleg
  • 9,131
  • 2
  • 44
  • 56
yli
  • 2,520
  • 5
  • 24
  • 31

7 Answers7

144

The problem with ps -efl | grep PORT_NUMBER is that PORT_NUMBER may match other columns in the output of ps as well (date, time, pid, ...). A potential killing spree if run by root!

I would do this instead :

PORT_NUMBER=1234
lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill 

Breakdown of command

  • (lsof -i tcp:${PORT_NUMBER}) -- list all processes that is listening on that tcp port
  • (awk 'NR!=1 {print $2}') -- ignore first line, print second column of each line
  • (xargs kill) -- pass on the results as an argument to kill. There may be several.
Shawn Chin
  • 79,172
  • 18
  • 156
  • 188
  • 1
    Fuser (below) is simpler – Federico Sep 28 '16 at 20:12
  • 11
    This is great. Got a lovely little alias now: `killport() { lsof -i tcp:$1 | awk 'NR!=1 {print $2}' | xargs kill }` killing off by port is now simply a case of writing e.g.: `killport 3984` – Ralph Cowling Dec 09 '16 at 12:40
  • 4
    Look at DanS's answer below (http://stackoverflow.com/a/12295745/67824). You can do without the `awk` if you use the `-it` option that was intended exactly for that: *-t specifies that lsof should produce terse output with process identifiers only and no header - e.g., so that the output may be piped to kill(1).*. Needless to say, that would be a better and more reliable approach. – Ohad Schneider Jan 26 '17 at 16:33
  • can someone please provide a stunt we can use when we have more than one port involved, for example when nginx is running 100 processes on many ports. Thanks. – nyxee Sep 11 '17 at 14:14
  • just an addition: ```A=1234; B=4567; lsof -i tcp:${A} -i tcp:${B} -t | xargs kill ``` – nyxee Sep 11 '17 at 14:33
  • -n make lsof faster, uniq kill only once per process: lsof -n -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' |uniq | xargs kill – Yu Jiaao Mar 08 '18 at 10:14
  • Not works for websocket ports, instead this works `sudo kill $(sudo lsof -t -i:1234)` – Subhod I Aug 28 '18 at 06:54
  • The `kill` isn't really killing that process in my case, I have to use `kill -9`. – Jason Liu Sep 06 '18 at 18:11
  • Thank you very much for this solution, it works great when executed from the terminal. But sadly enough, when i execute this from a script (.sh file) it won't work (and won't issue any error), as the process won't be killed. – Nisim Naim Jul 15 '20 at 06:25
29

1.) lsof -w -n -i tcp:8080

2.) kill -9 processId

thegrunt
  • 1,034
  • 1
  • 11
  • 22
  • 3
    Said many times here, don't use `kill -9`, just try `kill` or use `kill -15` what does SIGTERM and if doesn't work force it with `kill -9` finally. – m3nda Jan 29 '16 at 01:35
23
kill $( lsof -i:6000 -t )

Or if you need permissions:

sudo kill $( sudo lsof -i:6000 -t )
Ohad Schneider
  • 35,000
  • 11
  • 158
  • 195
DanS
  • 16,510
  • 8
  • 49
  • 46
  • This will work in most cases, but it seems like there are edge cases where multiple processes can listen on the same port, where the above will fail: http://stackoverflow.com/questions/1694144/can-two-applications-listen-to-the-same-port. – Ohad Schneider Jan 26 '17 at 16:31
  • i had nginx running multiple processes on the same port. – nyxee Sep 11 '17 at 14:04
  • (+1) for great answer there. I was wondering how could I add a test condition before it. I mean, I would like to first check if the port is actually being used by a process. So, the script would be something like : `test && kill $( lsof -i:6000 -t )`. Any suggestions would be helpful. – oblivion Nov 24 '17 at 05:46
20

Propose to use fuser command:

fuser -k -TERM -n tcp ${PORT_NUMBER}
Michael Zarubin
  • 366
  • 1
  • 4
12
sudo fuser -k 8080/tcp

An easy one to remember.

This syntax is probably much more recent than the date of the question!

NVRM
  • 8,789
  • 1
  • 69
  • 80
3
... | awk '{ print $4 }' | xargs kill -9

please test with "echo" instead of "kill" before running

Taher Khorshidi
  • 5,168
  • 5
  • 31
  • 53
vmpstr
  • 4,941
  • 2
  • 24
  • 25
2

To kill all processes listening on a particular port, e.g. port 8864

kill -9 $ \`lsof -i:8864 -t\`

Replace 8864 by the port you want.

Shawn Chin
  • 79,172
  • 18
  • 156
  • 188