Thursday, September 2, 2010

Script to find the PID of the Process which ps command cannot list.

Sometime we may need some command or code or logic to find the process id of the process when every other command fails to do that, like if you use ps -ef | grep then you will fail to see the process, but the same time if the process was using some port for example 8888, then after killing the process by kill -9 or shutting down the process by using specific shutdown scripts, the port will be still active at that time you will fail to bind this port with new process of same server/instance.

for that we can use following shell script to find out the socket process which are still active on the ports, it will list by process id.

#!/bin/bash



# is the port we are looking for

if [ $# -lt 1 ]
then


echo "Please provide a port number parameter for this script"


echo "e.g. %content 8888?"
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


echo ""


fi

done

Any comments ?? / Suggestions ??
 
Thanks
Shailesh

$ Symbol in UNIX - Dollar Symbol in Unix

Many of us still wonder about what $ symbol stands for, some of us know that $ stands for shell prompt for user and # stands for shell prompt for root in many Unix systems, but we have different meanings for different types of symbols associated with $ as below.

$ Symbol is used in UNIX as variable in sometimes and sometimes its used to define some variables.

In first case we have many variables associated with $ as below.

$? = Stores last commands exit Status
$$ = Stores PID of the current process.
S# = Stores number of passed parameters
$*/$@ = Lists all the passed parameters
$0 = Stores the Program or Script name
$1 - $9 = Stores the first 9 Parameters passed to the script.

For Second case :

We have some variables set for environment like

$PATH
$SHELL
$USER ... etc.


and we can define the values using $  Symbol once

bash-3.00$ x=1

bash-3.00$ echo $x
1
bash-3.00$

Thanks
Shailesh Dyade