How do I find (and kill) process running on a certain port?

30

14

Possible Duplicate:
Finding the process that is using a certain port in Linux

I'm using Ubuntu Linux 11.04. How do I write a shell script expression that will find the process running on port 4444 and then kill the process?

Dave

Posted 2011-08-12T13:15:29.657

Reputation:

Question was closed 2011-08-12T15:43:03.840

I don't think this is a duplicate as it is asking how to kill, not find, the process on the port: fuser -k 9000/tcp – Kris – 2012-07-19T15:39:41.367

Answers

48

You could use lsof to find the process:

lsof -t -i:4444

would list only the pid of the process listening on port 4444. You could just say

kill `lsof -t -i:4444`

if you were brave.

Ernest Friedman-Hill

Posted 2011-08-12T13:15:29.657

Reputation: 855

15+1 for 'if you were brave.' – Mr. Shickadance – 2011-08-12T13:23:19.483

7

You use lsof:

# lsof -n | grep TCP | grep LISTEN | grep 4444

The output will be something like:

pname 16125 user 28u IPv6 4835296 TCP *:4444 (LISTEN)

Where the first column is the process name, and the second column is the process id. You then parse the output, find out what the process id (PID) is and use kill command to kill it.

Pablo Santa Cruz

Posted 2011-08-12T13:15:29.657

Reputation: 1 625

everything in linux is a file and lsof lets you find files so... yeah, very useful – jcollum – 2019-06-07T15:38:29.940

1I'd never heard of lsof before. Looking at the man page, it appears to be incredibly useful. Thanks! – None – 2011-08-12T13:26:30.710

2

Alternatively you could use netstat -ap if lsof is not available on you system (as it isn't on a busybox system I work with regularly).

DaveRandom

Posted 2011-08-12T13:15:29.657

Reputation: 199

good call, I was wondering why this wasn't working on a busybox derived docker image I was working on – jcollum – 2019-06-07T15:39:23.040

2

kill -9 `netstat -lanp --protocol=inet | grep 4444 | awk -F" " '{print $7}' | awk -F"/" '{print $1}'`

Uses netstat to list listening INET sockets with numeric ports and parent processes. Filters for string 4444, takes out the 7th column( pid/process name ) and further splits it by "/" to get the pid. Passes that to kill command.

Arek B.

Posted 2011-08-12T13:15:29.657

Reputation: 121

2I'd recommend against the kill -9. It doesn't allow cleanup, and some internet app is more likely to have resources that need to be shut down cleanly. – Rich Homolka – 2011-08-12T16:50:18.467