Tuesday, November 5, 2013

Find the Java Process using Port (Solaris/Linux)

Find Which Process Binded to my Port:

                                                                                                                                                  
There are lot of instances whereWe will come across a situation where we need to chase to find the process
for a give port , if there are 20-30 of java process running  on box.

Developers are no clue whatz going , what process they are killing what process they are starting etc etc

Here is a sample script which will take port as input and give us the process id

#!/bin/bash

if [ $# -lt 1 ]
then
echo "Usage: getpid.sh |port| "
echo "Please retry with correct options"
echo "if you want to suppress the output with unnecessary string like 'permission denied'"
echo "Use: ./getpid.sh |port| > 2>/dev/null "
exit
fi

echo "Scanning for your port............!!!!!!!!!!!!!!"

ls /proc | while read i
do
pfiles $i | grep AF_INET | grep $1 |grep peername
if [ $? -eq 0 ]
then
echo Is owned by pid $i
echo
fi
done
echo "Script Completed Successfully..."
echo 
./getSolarisPid.sh 12082 
./getSolarisPid.sh 12082 2>/dev/null (if you want to supress the permission denied messgs')














 LINUX

netstat -tulanp |grep <pid>




Find the process id and grep using ps

ps -ef |grep 11817











Find HighCpu Threads on Weblogic Linux

FIND HIGH CPU ON LINUX with JDK


Lot of times on the WLS PROD Environment we see high CPU on the java process , We just end up restarting the server , Its very hard to find the root cause


Here is small utility script that will display the Top 10 cpu consuming threads of the java process we just need to pass the <Java PID> to the script.


cpuhigh.sh

 Just copy the script to  a sh file

./cpuhigh.sh <javaPID>



#!/bin/bash

# put your JAVA_HOME here,
JAVA_HOME=/JDK/jdk160_31

PID=$1
IFS=''
top_number=10
if [ $# -gt 1 ] ; then
top_number=$2
fi

top_number=$((top_number+1))

java_stack=`$JAVA_HOME/bin/jstack -l $PID`

top=`top -s -b -H -p $PID -n 1 | grep -vE '^top|^Tasks|^Cpu|^Mem|^Swap|^$' | awk 'NR==1; NR > 1 {print $0 | "sort -nrk 9"}' | head -$top_number`
echo $top

echo $top | while read psline; do
if [ `echo $psline|grep -c PID` -gt 0 ] ;
then continue
fi
lwp_id=`echo $psline | awk '{print $1}'`
nid=`printf '%x\n' $lwp_id `
echo "========> Java LWP: $lwp_id - Native Thread ID=$nid"
echo $java_stack | sed -n "/$nid/,/^$/ p" ;
done



Sample Output