It's not a secret anymore!*
The moose likes Linux / UNIX and the fly likes What pid is using what port Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "What pid is using what port" Watch "What pid is using what port" New topic
Author

What pid is using what port

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

We use cloverleaf to route messages. Sometimes, for reasons as yet unknown to us, the wrong process grabs a port. Then, when the correct process tries to start, it can't have the port it wants, so it won't start. We are then left with the task of finding which PID has the port in question so we can shut it down, start the correct one, then re-start the bad one.

We're on SunOS 5.10. From what I've been able to find, "lsof" doesn't work right on this version.

The ONLY think I've found is a perl script that reads the /proc directory to get a list of every running PID. It then does a

pfiles $pid | grep AF_INET

for every PID found. We may have 1500-2000 PIDs on a machine, so this can take a while.

Does anyone have any ideas or suggestions on how I might be able to speed this up, or a different approach to take?



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5830
    
    7

Is netstat available on SunOS? On Linux, "netstat -p" lists the ports for each process.


JBoss In Action
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Install sockstat, then use that: Alternatively: (drop the "u" if you don't need UDP). Use grep to filter out the port, then use a tool like awk to get the desired field(s). You may need to be root for these commands to list all the ports; sockstat doesn't recognize my MySQL server unless I use sudo, and netstat doesn't list the PID / program.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Another command to base your final command on: Which leads me to this: The -t will make lsof print only the PID; the -i:<port> (or -i:<port,port,port> for multiple) filters out anything you don't need.

Again, requires root access to get any useful information.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

According to my research:

On Solaris 10, using "lsof -i" to show mapping of processes to TCP ports incorrectly shows all processes that have socket open as using port 65535


netstat -p returns this:

mars:hci:/opt/quovadx/qdx5.7/integrator/sitena>netstat -p
Net to Media Table: IPv4
Device IP Address Mask Flags Phys Addr
------ -------------------- --------------- ----- ---------------
ce0 ntp.mcast.net 255.255.255.255 01:00:5e:00:01:01
ce0 upwrhlab01.carenet.org 255.255.255.255 00:09:6b:dd:ab:98
ce0 medmgrdv2.carenet.org 255.255.255.255 00:04:ac:97:65:ae
ce0 cisyellow.carenet.org 255.255.255.255 00:02:55:5a:0b:d8
ce0 utschmis01.carenet.org 255.255.255.255 00:03:ba:2c:da:44
...

-l, -t, and -u are not valid flags for me.


usage: netstat [-anv] [-f address_family]
netstat [-n] [-f address_family] [-P protocol] [-g | -p | -s [interval [count]]]
netstat -m [-v] [interval [count]]
netstat -i [-I interface] [-an] [-f address_family] [interval [count]]
netstat -r [-anv] [-f address_family|filter]
netstat -M [-ns] [-f address_family]
netstat -D [-I interface] [-f address_family]


I know next to nothing about networking, so most of this is meaningless to me.

I'll look for sockstat, and see if that might be an option.

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You can mimic the -t by grepping on ^tcp; netstat -nap should help you. The following works on one of my older machines to list the details for SSH:
Combine that with awk:
$7 is the PID/program field, so the substr / index part gets the PID only, stripping off the program name. (awk uses 1 as the first index. Stupid program.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What pid is using what port