Thursday, October 7, 2010

How to find PID associated with a Port number

Scenario
At times we may run into situation to find the PID associated with a particular Port number. We may not be able to start/restart a particular service (say Jboss), since the Port number which is needed for the service is occupied by another process. You would have seen the error saying “Address already in use”. 

Solution:
Find the PID associated with the particular port and kill it, if not required, and start the service.

Couple of straight forward commands:

# lsof -i      (or)  lsof -i: <port number>

# netstat -lantp   (or)  netstat -lautp

Examples:

[root@hostx ~]# lsof -i:80                        # Second column shows the PID associated with HTTPD service running on port 80.
COMMAND   PID   USER   FD   TYPE   DEVICE SIZE NODE NAME
httpd   28064   root    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28066 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28067 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28068 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28069 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28070 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28071 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28072 apache    4u  IPv6 27763387       TCP *:http (LISTEN)
httpd   28073 apache    4u  IPv6 27763387       TCP *:http (LISTEN)                                                                                                                         

[root@hostx ~]# lsof -i:123                   # 1582 is the PID for NTP service running on port 123
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
ntpd    1582  ntp   16u  IPv4   3830       UDP *:ntp
ntpd    1582  ntp   17u  IPv6   3831       UDP *:ntp
ntpd    1582  ntp   18u  IPv6   3846       UDP [::1]:ntp
ntpd    1582  ntp   19u  IPv6   3847       UDP [fe80::3c8c:7ff:fe96:2705]:ntp
ntpd    1582  ntp   20u  IPv6   3890       UDP [fe80::1c43:d9ff:fe07:dfc0]:ntp
ntpd    1582  ntp   21u  IPv4   3964       UDP localhost.localdomain:ntp
ntpd    1582  ntp   22u  IPv4   3965       UDP kesav01-t2:ntp

 [root@hostx ~]# netstat -lantp | grep java      # 3rd column shows the Local server Port number and last column shows the PID and process associated with it.
tcp        0      0 0.0.0.0:55980               0.0.0.0:*                   LISTEN      20586/java
tcp        0      0 0.0.0.0:11025               0.0.0.0:*                   LISTEN      20586/java
tcp        0      0 10.10.64.78:8088            0.0.0.0:*                   LISTEN      20607/java
tcp        0      0 10.10.64.78:8089            0.0.0.0:*                   LISTEN      20586/java
tcp        0      0 0.0.0.0:42587               0.0.0.0:*                   LISTEN      20586/java
tcp        0      0 0.0.0.0:55933               0.0.0.0:*                   LISTEN      20607/java
tcp        0      0 10.10.64.78:39646           10.10.25.32:55200           ESTABLISHED 20607/java
tcp        0      0 10.10.64.78:39645           10.10.25.32:55200           ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:50681           10.10.64.90:1521            ESTABLISHED 20607/java
tcp        0      0 10.10.64.78:50683           10.10.64.90:1521            ESTABLISHED 20607/java
tcp        0      0 10.10.64.78:50682           10.10.64.90:1521            ESTABLISHED 20607/java
tcp        2      0 10.10.64.78:60506           10.10.65.13:8088            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:42882           10.10.64.90:1521            ESTABLISHED 20607/java
tcp        0      0 10.10.64.78:42881           10.10.64.90:1521            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:48708           10.10.64.90:1521            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:48709           10.10.64.90:1521            ESTABLISHED 20607/java
tcp        0      0 10.10.64.78:48706           10.10.64.90:1521            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:48707           10.10.64.90:1521            ESTABLISHED 20607/java
tcp        0      0 10.10.64.78:48704           10.10.64.90:1521            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:48705           10.10.64.90:1521            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:48703           10.10.64.90:1521            ESTABLISHED 20586/java
tcp        0      0 10.10.64.78:8088            10.10.65.16:51863           ESTABLISHED 20607/java
[root@hostx ~]#

[root@hostx ~]# netstat -lautp | grep java       # Difference with ‘u’ option is, it resolves the Hostname and Port number.
tcp        0      0 *:55980                     *:*                         LISTEN      20586/java
tcp        0      0 *:11025                     *:*                         LISTEN      20586/java
tcp        0      0 kseav01-t2:radan-http      *:*                         LISTEN      20607/java
tcp        0      0 kesav01-t2:8089            *:*                         LISTEN      20586/java
tcp        0      0 *:42587                     *:*                         LISTEN      20586/java
tcp        0      0 *:55933                     *:*                         LISTEN      20607/java
[root@hostx ~]#

No comments:

Post a Comment