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
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.
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.
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.)