Thursday 18 April 2013

Find out the process id without having to install lsof

This abstract illustrates the steps to find out the process id by using the port number without having to install lsof that may help the AIX System Administrators .

The past experiences have shown troubles to compile the source in certain versions of AIX(v5.3) and otherwise. Therefore the attached script will help achieve the same as lsof utility does.

Some times we know the port number and may want to kill the process manually. This can be achieved using lsof command. However, past experiences show that lsof command has troubles installing/compiling particularly with certain versions of AIX. Sometimes the source of lsof compiles properly and most of the times it does not.

Therefore, to get away the use of lsof command the steps mentioned in the solution description will help find out the process id without hassles.

Before you begin to execute this script you should know the port number and check if the port number is being listened. Use the command :

netstat -an | grep "<port_number>"
below posted  "processid_from_portno.sh"  script which accepts port number as input and gives process id as output. 

Step 1. Find out the address of the port number
# netstat -Aa | pg
output will be something like :
f100060003606b98 tcp4 0 0 *.51063 *.* LISTEN
f100060003489398 tcp4 0 0 *.51100 *.* LISTEN
f1000600035ed398 tcp4 0 0 *.51101 *.* LISTEN
Step 2. Take the hex address for the port number 
e.g. 51063
Step 3. Use sockinfo command
# echo "sockinfo f100060003606b98 tcpcb" | kdb |grep proc
the output will be something like this :
F100070F00000000 F100070F10000000 pvproc+000000
proc/fd: 16463/562
proc/fd: fd: 562
pvproc+1013C00 16463*java ACTIVE 004F1E4 0000001 0000000147AF6400 0 0037
Step 4. Convert the hex value to decimal
# echo "hcal 004F1E4"|kdb|grep Value
output will be:
Value hexa: 0004F1E4 Value decimal: 324068
Step 5. Find out the process id now
# ps -fp 324068
output will be:
UID PID PPID C STIME TTY TIME CMD
root 324068 1 0 Jan 17 - 8:06 /usr/WebSphere/p11/AppServer//java/bin/java -Djava.net.preferIPv4Stack=t
----------------

The Script

#!/bin/ksh
###############################################################################
#
###############################################################################
#
# File : processid_from_portno.sh
#
# Accepts input as port number and gives process id details as output
#
###############################################################################

echo ""
echo "Type the port number and press Enter : "
read port
# Check for input. Exit if no input
if [ "$port" = "" ]
then
echo ""
echo "Exiting.....port number not entered. please re-execute the command"
echo ""
exit 1
fi
# get address of the port number
export ADD_VAL=$(netstat -Aa | grep -w ${port} | awk '{print $1}')
if [ "$ADD_VAL" = "" ]
then
echo ""
echo "port number ${port} is not in use"
echo ""
exit 1
fi
# get the hex value of
export HEX_VAL=$(echo "sockinfo ${ADD_VAL} tcpcb" | kdb |grep proc | awk '{print $4}')
# This is optional for additional check
# if [ "$HEX_VAL" = "" ]
# then
# echo ""
# echo "error reading hex value of a port number ${port}"
# echo ""
# exit 1
# fi
# convert hex value to decimal
export DEC_VAL=$(echo "16i${HEX_VAL} p" | dc)
# This is optional for additional check
# if [ "$DEC_VAL" = "" ]
# then
# echo ""
# echo "error converting hex value to decimal for port number ${port}"
# echo ""
# exit 1
# fi
echo "-----------------PROCESS ID------------------------"
echo $(ps -fp ${DEC_VAL} | awk '{ print $1 " " $2 " " $3 }')
echo "---------------------------------------------------"
---------------------------------------

0 blogger-disqus:

Post a Comment