Monthly Archives: February 2015

How to find process ID listening on a given port on solaris?

On solaris it is not as simple to find out process id listening on a particular port as in linux using ps -eafp|grep PORT

#!/bin/bash
if [ $# -lt 1 ]
then
echo "Please provide port number parameter"
echo "e.g. 9043"
exit
fi
input=$1
userid=""
if [ $# -eq 2 ]
then
	userid=$2
fi

if [ "$userid" != "" ]
then
	procs=`ls -lhtr /proc | grep $userid | awk '{print $9}'`
else
	procs=`ls /proc`
fi

for i in $procs
do
p=`pfiles $i | grep AF_INET | grep $input | tail -1 | awk '{print $5}'`
if [ "$p" -eq "$input" ]
then
echo "************************"
echo "* $p is owned by pid $i *"
echo "************************"
break;
fi
done

Examples:

wasadmin@testbox01:~$ sh /tmp/getpid.sh 9043 wasadmin
************************
* 9043 is owned by pid 14225 *
************************
wasadmin@testbox01:~$

If you run the script as non root user and current user doesn’t have permissions to to a process you might see “permission denied” messages as below

wasadmin@testbox01:~$ sh /tmp/getpid.sh 9043
pfiles: permission denied: 12341
pfiles: permission denied: 12651
pfiles: permission denied: 12722
pfiles: permission denied: 12731
pfiles: permission denied: 12859
pfiles: permission denied: 12880
pfiles: permission denied: 12894
pfiles: permission denied: 13983
************************
* 9043 is owned by pid 14225 *
************************
Array ( [marginTop] => 100 [pageid] => @techish1 [alignment] => left [width] => 292 [height] => 300 [color_scheme] => light [header] => header [footer] => footer [border] => true [scrollbar] => scrollbar [linkcolor] => #2EA2CC )