:: MArX'' Frustrated communist, card-carrying geek.

22Jun/100

Get application pid listening on a network port on Solaris

This week, a coworker asked my opinion about a problem. He needed to startup a service, but the port which this service needed to bind was already in use.

The problem seemed simple, figure out which process was binded on this port, check if it was an stuck instance of the same service previously ran or a legitimate service and then take appropriate action.

Usually the netstat command has a parameter to inform the process associated to the port, it's -p on Linux, -b on windows and so on. But, what about Solaris? Damn! Solaris's netstat hasn't this feature.

I found away various ideas to solve the problem, but all they using one of two approaches, lsof or pfiles. The lsof isn't built-in on Solaris, so only left me pfiles.

The below script does the dirty job, isn't a pretty thing to grep a bunch of proc files, but it works (if you have access to do that).

#!/bin/bash
# Get the process which listens on port
# $1 is the port we are looking for

if [ $# -lt 1 ]
then
echo "Please provide a port number parameter for this script"
echo "e.g. $0 22"
exit
fi

echo "Greping for your port, please be patient (CTRL+C breaks) ... "

for i in `ls /proc`
do
pfiles $i | grep AF_INET | grep $1
if [ $? -eq 0 ]
then
echo Is owned by pid $i
fi
done

I hope it helps you too.

Filed under: Solaris Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

 

No trackbacks yet.