Skip to content

Commit e0f9930

Browse files
committed
增加对非root用户启动的jvm进程的支持
假设当前系统上有3个JVM进程 ``` pid1 userA pid2 userB pid3 userC ``` cpu 使用率可能是这样的情况 ``` pid1 tid11 userA 30.0 pid1 tid12 userA 15.0 pid2 tid13 userB 10.0 pid3 tid14 9.0 ``` 按照之前的脚本方式 1 如果是root用户直接执行 show-busy-java-threads.sh 是不行的‘ 2 如果 sudo -u userA show-busy-java-threads.sh 那么 userB userC的jstack的信息是取不到的 所以只能把sudo -u user 放在脚本里判断 如果是非root用户执行这个脚本,必须 sudo show-busy-java-threads.sh
1 parent c015835 commit e0f9930

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

show-busy-java-threads.sh

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ Options:
1919
-p, --pid find out the highest cpu consumed threads from the specifed java process,
2020
default from all java process.
2121
-c, --count set the thread count to show, default is 5
22-
-u, --user set java process user
2322
-h, --help display this help and exit
2423
EOF
2524
exit $1
2625
}
2726

28-
readonly ARGS=`getopt -n "$PROG" -a -o c:p:u:h -l count:,pid:,help -- "$@"`
27+
readonly ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`
2928
[ $? -ne 0 ] && usage 1
3029
eval set -- "${ARGS}"
3130

@@ -39,10 +38,6 @@ while true; do
3938
pid="$2"
4039
shift 2
4140
;;
42-
-u|--user)
43-
jvmuser="$2"
44-
shift 2
45-
;;
4641
-h|--help)
4742
usage
4843
;;
@@ -90,39 +85,34 @@ trap "cleanupWhenExit" EXIT
9085
printStackOfThread() {
9186
local threadLine
9287
local count=1
93-
while read threadLine ; do
94-
local pid=`echo ${threadLine} | awk '{print $1}'`
95-
local threadId=`echo ${threadLine} | awk '{print $2}'`
88+
while IFS=" " read -a line ; do
89+
local pid=${line[0]}
90+
local threadId=${line[1]}
9691
local threadId0x=`printf %x ${threadId}`
97-
local user=`echo ${threadLine} | awk '{print $3}'`
98-
local pcpu=`echo ${threadLine} | awk '{print $5}'`
92+
local user=${line[2]}
93+
local pcpu=${line[4]}
9994

10095
local jstackFile=/tmp/${uuid}_${pid}
101-
102-
103-
[ ! -f "${jstackFile}" ] && {
104-
if [ `uname -m` == "x86_64" ];then
105-
flag="-J-d64" # x86-64 specify jstack option "-J-d64"
106-
fi
107-
if [ -z ${jvmuser+x} ];then
108-
# root user
109-
jstackCmd="$JAVA_HOME/bin/jstack $flag"
110-
else
111-
# non-root user use sudo -u user
112-
jstackCmd="sudo -u $jvmuser $JAVA_HOME/bin/jstack $flag"
113-
fi
114-
$jstackCmd ${pid} > ${jstackFile} || {
115-
redEcho "Fail to jstack java process ${pid}!"
116-
rm ${jstackFile}
117-
continue
118-
}
119-
}
12096

97+
[ ! -f "${jstackFile}" ] && {
98+
{
99+
if [ "${user}" == "${USER}" ];then
100+
jstack ${pid} > ${jstackFile}
101+
else
102+
sudo -u ${user} jstack ${pid} > ${jstackFile}
103+
fi
104+
} || {
105+
redEcho "Fail to jstack java process ${pid}!"
106+
rm ${jstackFile}
107+
continue
108+
}
109+
}
121110
redEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
122111
sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
123112
done
124113
}
125114

115+
126116
ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
127117
[ -z "${pid}" ] &&
128118
awk '$4=="java"{print $0}' ||

0 commit comments

Comments
 (0)