Skip to content

Commit

Permalink
fixed condition where passing in pid results in finding /bin/bash
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Herrick committed Feb 11, 2016
1 parent 8d4a413 commit c4c2cc6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions bash_scripts/threads_by_cpu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ if [ -z "$1" ]
topprocutil=`ps --no-headers -eo pcpu,pid,args | tr -s " " | sed 's/^ *//g' | sort -n -r | cut -d' ' -f1 | head -1`
else
topprocpid=$1
topprocname=`ps --no-headers -eo pcpu,pid,args | grep $1 | grep -v grep | tr -s " " | sed 's/^ *//g' | cut -d' ' -f3`
topprocutil=`ps --no-headers -eo pcpu,pid,args | grep $1 | grep -v grep | tr -s " " | sed 's/^ *//g' | cut -d' ' -f1`
topprocname=`ps --no-headers -eo pcpu,pid,args | grep $1 | grep -v grep | grep -v $0 | tr -s " " | sed 's/^ *//g' | cut -d' ' -f3`
topprocutil=`ps --no-headers -eo pcpu,pid,args | grep $1 | grep -v grep | grep -v $0 | tr -s " " | sed 's/^ *//g' | cut -d' ' -f1`
fi
if echo $topprocname | grep -q java;
then
Expand All @@ -26,8 +26,8 @@ if echo $topprocname | grep -q java;
i=$(( $i + 1));
done
#Try to find jstack from the process arguments and take a thread dump. This is a best-effort kind of deal, obviously won't work in implementations that don't have jstack, like Websphere.
javabin=$(ps -eo pid,args | grep ${topprocpid} | grep -v grep | tr -s " " | sed 's/^ *//' | cut -d " " -f 2 | rev | cut -d "/" -f2- | rev)
javauser=$(ps -eo pid,user | grep ${topprocpid} | grep -v grep | sed 's/^ *//' | cut -d " " -f 2)
javabin=$(ps -eo pid,args | grep ${topprocpid} | grep -v grep | grep -v $0 | tr -s " " | sed 's/^ *//' | cut -d " " -f 2 | rev | cut -d "/" -f2- | rev)
javauser=$(ps -eo pid,user | grep ${topprocpid} | grep -v grep | grep -v $0 | sed 's/^ *//' | cut -d " " -f 2)
printf "\n Attempting to dump PID ${topprocpid} using jstack in ${javabin} as user ${javauser} \n\n"
su - ${javauser} -c "${javabin}/jstack -l ${topprocpid}"
else
Expand Down

0 comments on commit c4c2cc6

Please sign in to comment.