Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
增加对非root用户启动的JVM的支持
如果是非root用户启动的JVM,使用这个脚本会报错

3031: well-known file is not secure
Fail to jstack java process 3031!
  • Loading branch information
liuyangc3 committed Nov 9, 2015
commit c015835ce634603f93b3ce9ed0e9b1e7ff97b683
32 changes: 24 additions & 8 deletions show-busy-java-threads.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ Options:
-p, --pid find out the highest cpu consumed threads from the specifed java process,
default from all java process.
-c, --count set the thread count to show, default is 5
-u, --user set java process user
-h, --help display this help and exit
EOF
exit $1
}

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

Expand All @@ -38,6 +39,10 @@ while true; do
pid="$2"
shift 2
;;
-u|--user)
jvmuser="$2"
shift 2
;;
-h|--help)
usage
;;
Expand Down Expand Up @@ -93,14 +98,25 @@ printStackOfThread() {
local pcpu=`echo ${threadLine} | awk '{print $5}'`

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



[ ! -f "${jstackFile}" ] && {
jstack ${pid} > ${jstackFile} || {
redEcho "Fail to jstack java process ${pid}!"
rm ${jstackFile}
continue
}
}
if [ `uname -m` == "x86_64" ];then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在 x86_64 的机器上测试了, 并不需要 jstack 命令上 加上 -J-d64 选项。

@liuyangc3 你不加这个选项 碰到的问题是什么?给一下相关的信息 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我测试的时候也发现确实不需要这个选项,oracle官方doc上指出在64位系统上可能需要指定这个参参数,这里确实画蛇添足了

flag="-J-d64" # x86-64 specify jstack option "-J-d64"
fi
if [ -z ${jvmuser+x} ];then
# root user
jstackCmd="$JAVA_HOME/bin/jstack $flag"
else
# non-root user use sudo -u user
jstackCmd="sudo -u $jvmuser $JAVA_HOME/bin/jstack $flag"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关于 root 用户 的java进程,普通用户 jstack 会失败 问题,
我个人的推荐的做法是,直接用sudo 去 执行这个命令:

# root 用户执行
sudo /path/to/show-busy-java-threads.sh

# 或是指定用户
sudo -u user_foo /path/to/show-busy-java-threads.sh

这样,脚本本身不用关心切换用户的问题,而是和 sudo命令 组合解决:

  • show-busy-java-threads.sh和其它命令(如 rm、jstack等等)一样,不去自己处理用户切换。
  • 多数情况下,执行show-busy-java-threads.sh的用户 和 Java进程是同一用户。

@liuyangc3 你看这样做如何?会不会更常规方便些?

你说的『关于 root 用户 的java进程,普通用户 jstack 会失败 问题』大家确实会碰到,很有意义!

如果觉得这样OK,你能再给个Pull Request不?更新一下show-busy-java-threads.sh的文档,说明一下这个问题,这样大家都看到你的提交和改进。 😸

非常感谢你的贡献!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赞 ,我一般都是习惯了脚本里写sudo,其实就是懒得每次敲sudo ,太懒。。
确实应该如您所说的,应该sudo放在脚本外面,这样更加规范。
我试试 Pull Request 一个用户判断的提示吧

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool~ ✨

fi
$jstackCmd ${pid} > ${jstackFile} || {
redEcho "Fail to jstack java process ${pid}!"
rm ${jstackFile}
continue
}
}

redEcho "[$((count++))] Busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) stack of java process(${pid}) under user(${user}):"
sed "/nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
Expand Down