1. Finding the process that holds the port
lsof -i <Internet address>
Let's find the process using the port "8080"
$ lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 14124 kamal 35u IPv4 18493 0t0 TCP *:http-alt (LISTEN)
If needed, we can extract just the PID by leaving out all other information as shown below.
$ lsof -i :8080 -t
14124
PID above is the process id that uses the port 8080.
Using this PID, you can view the process details using following command.
ps u -p <PID>
Let's find the process details of PID 14124
$ ps u -p 14124
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
kamal 14124 6.6 7.2 2399340 588220 pts/0 Sl+ 10:39 1:01 /usr/lib/jvm/java-6-sun/bin/java
2. Terminating the process using the process id
A process can be terminated using the kill command.kill -9 <PID>
Let's kill the above process as follows.
$ kill -9 14124
Hope this helps, also make sure you read details of "lsof" command for further details.
Good to know lsof :)
ReplyDeleteI always do a "netstat -apn | grep " to get the pid of the process but netstat output is kind of verbose.
Aj
Yes exacty, netstat was my favourite until I came across lsof. :)
ReplyDelete