Blog dedicated to Oracle Applications (E-Business Suite) Technology; covers Apps Architecture, Administration and third party bolt-ons to Apps

Thursday, August 2, 2007

How to find the pid of a process which is using a specific port

The lsof unix command can give you the detail. lsof stands for list of open files. lsof command lists information about files opened by

An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file (Internet socket, NFS file or UNIX domain socket.) A specific file or
all the files in a file system may be selected by path.

In Solaris lsof is present in /usr/local/bin/lsof usually.

lsof | grep will return an output like this one:

lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME


Good document for finding out the same information on Linux is: http://www.redhat.com/docs/manuals/linux/RHL-9-Manual/security-guide/s1-server-ports.html

For HP-UX, lsof is not part of the standard install. You can download it from:

http://ftp.cerias.purdue.edu/pub/tools/unix/sysutils/lsof/binaries/hpux/

Solaris 10 has issues with lsof version 4.77. Ensure that you are using lsof version 4.8 on Solaris 10 to get correct results.

If you don't have lsof on Solaris this My Oracle Support (erstwhile Metalink) Article has a script

When "lsof" Utility Is Not Installed, How To Determine Which Process is Using a TCP Port on Sun Solaris Platforms? [ID 839919.1]

This gives a script get_port_pid.sh:

#!/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
[ -d /proc/$i ] && pfiles $i | grep AF_INET | grep $1
if [ $? -eq 0 ]
then
echo Is owned by pid $i
fi
done


No comments: