How to find port-based processes and kill them all?

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?

+45
bash shell
Feb 18 '11 at 16:23
source share
7 answers

Problem with ps -efl | grep PORT_NUMBER ps -efl | grep PORT_NUMBER is that PORT_NUMBER can also match other columns in ps output (date, time, pid, ...). Potential kill on root!

I would do this instead:

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

Team Breakdown

  • ( lsof -i tcp:${PORT_NUMBER} ) - a list of all processes that are listening on this tcp port
  • ( awk 'NR!=1 {print $2}' ) - ignore the first row, print the second column of each row
  • ( xargs kill ) - pass the results as an argument to kill . There may be several.
+91
Feb 18 '11 at 16:31
source share
β€” -

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

2.) kill -9 processId

+18
Sep 09 '13 at 8:38
source share

Suggest using the fuser command:

 fuser -k -TERM -n tcp ${PORT_NUMBER} 
+13
Feb 19 '11 at 2:23
source share
 kill $( lsof -i:6000 -t ) 

Or if you need permissions:

 sudo kill $( sudo lsof -i:6000 -t ) 
+8
Sep 06 '12 at 8:14
source share
 ... | awk '{ print $4 }' | xargs kill -9 

before starting

please test with "echo" instead of "kill"
+2
Feb 18 '11 at 16:26
source share

To kill all processes listening on a specific port, for example. port 8864

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

Replace 8864 with the desired port.

+1
Aug 23 '11 at 17:41
source share

sudo fuser -k 8080 / tcp

It's easy to remember one liner that works well on most or all Unix.

This syntax is probably much later than the date of the question.

0
May 09 '17 at 1:10 pm
source share



All Articles