Skip to content

Commit 7ddc8bc

Browse files
committed
Merge pull request oldratlee#50 from liuyangc3/master
增加对非root用户启动的JVM的支持
2 parents 1cb12d2 + 93305e2 commit 7ddc8bc

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ source <(curl -fsSL https://raw.githubusercontent.com/oldratlee/useful-scripts/m
2121

2222
1. [show-busy-java-threads.sh](docs/java.md#beer-show-busy-java-threadssh)
2323
打印出在运行的`Java`进程中,消耗`CPU`最多的线程栈。用于快速排查`Java`的性能问题。
24+
如果执行脚本的用户不是root,需要前面加上sudo
2425
1. [show-duplicate-java-classes](docs/java.md#beer-show-duplicate-java-classes)
2526
找出`jar`文件和`class`目录中的重复类。用于排查`Java`类冲突问题。
2627
1. [find-in-jars.sh](docs/java.md#beer-find-in-jarssh)

show-busy-java-threads.sh

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,36 @@ cleanupWhenExit() {
8383
trap "cleanupWhenExit" EXIT
8484

8585
printStackOfThread() {
86-
local threadLine
86+
local line
8787
local count=1
88-
while read threadLine ; do
89-
local pid=`echo ${threadLine} | awk '{print $1}'`
90-
local threadId=`echo ${threadLine} | awk '{print $2}'`
88+
while IFS=" " read -a line ; do
89+
local pid=${line[0]}
90+
local threadId=${line[1]}
9191
local threadId0x=`printf %x ${threadId}`
92-
local user=`echo ${threadLine} | awk '{print $3}'`
93-
local pcpu=`echo ${threadLine} | awk '{print $5}'`
92+
local user=${line[2]}
93+
local pcpu=${line[4]}
9494

9595
local jstackFile=/tmp/${uuid}_${pid}
9696

9797
[ ! -f "${jstackFile}" ] && {
98-
jstack ${pid} > ${jstackFile} || {
98+
{
99+
if [ "${user}" == "${USER}" ];then
100+
jstack ${pid} > ${jstackFile}
101+
else
102+
sudo -u ${user} jstack ${pid} > ${jstackFile}
103+
fi
104+
} || {
99105
redEcho "Fail to jstack java process ${pid}!"
100106
rm ${jstackFile}
101107
continue
102108
}
103109
}
104-
105110
redEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
106111
sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
107112
done
108113
}
109114

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

0 commit comments

Comments
 (0)