Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加对非root用户启动的JVM的支持 #50

Merged
merged 4 commits into from
Nov 14, 2015
Merged

增加对非root用户启动的JVM的支持 #50

merged 4 commits into from
Nov 14, 2015

Conversation

liuyangc3
Copy link
Contributor

如果是非root用户启动的JVM,使用这个脚本会报错

3031: well-known file is not secure
Fail to jstack java process 3031!

如果是非root用户启动的JVM,使用这个脚本会报错

3031: well-known file is not secure
Fail to jstack java process 3031!
@oldratlee
Copy link
Owner

👍 我看看!

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位系统上可能需要指定这个参参数,这里确实画蛇添足了

@liuyangc3
Copy link
Contributor Author

     我个人的推荐的做法是,直接用sudo 去 执行这个命令

这里我有个疑问

假设系统上有2个jvm进程
p1 用户 tomcat
p2 用户 root

如果使用root 运行jstack 是不能attach tomcat用户的进程的

那么在不指定pid运行脚本的形况下
从ps后面的命令来看,应该是统计所有jvm进程的线程

那么sudo -u tomact 放在脚本外面 jstack无法 attach root的进程

所以我觉得是不是应该在脚本内部去判断每个进程的 user
如果这个进程user 不是当前运行脚本的user ,就是用sudo 执行 jstack

假设当前系统上有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
oldratlee added a commit that referenced this pull request Nov 14, 2015
增加对非root用户启动的JVM的支持
@oldratlee oldratlee merged commit 7ddc8bc into oldratlee:master Nov 14, 2015
@oldratlee
Copy link
Owner

oldratlee commented Nov 15, 2015

那么sudo -u tomact 放在脚本外面 jstack无法 attach root的进程

所以我觉得是不是应该在脚本内部去判断每个进程的 user
如果这个进程user 不是当前运行脚本的user ,就是用sudo 执行 jstack

如你所说,因为root用户也不能jstack其它用户的Java进程,要在脚本切换用户。

多谢你的建议和测试! 👍
整个PR已经合并。 @liuyangc3

反馈你晚了,不好意思~

PS: 更多中间的讨论 参见 提交中的评论讨论

@oldratlee
Copy link
Owner

oldratlee commented Nov 15, 2015

改了一下脚本实现,

如果Java进程的用户 与 执行脚本的当前用户 不同,提示用户使用sudo再执行,不在脚本直接切换。

原因是:

避免在脚本做的sudo操作,需要用户输入root密码,而Block脚本(有卡死自动化系统的风险)。
在服务器上脚本自动执行,不Block的特点很重要,提高整体系统的 可靠性

对于 用户 手动执行的情况,显式地提示 给出了重新运行的命令行,使用便利些:

$ ./show-busy-java-threads.sh -c 6
[1] Busy(0.8%) thread(13706/0x358a) stack of java process(13705) under user(jerry):
"main" prio=10 tid=0x00007fc87c009000 nid=0x358a waiting on condition [0x00007fc882942000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at Hello.main(Hello.java:3)

[2] Fail to jstack Busy(0.7%) thread(13696/0x3580) stack of java process(13695) under user(root).
User of java process(root) is not current user(jerry), need sudo to run again:
    sudo ./show-busy-java-threads.sh -c 6 # 显式地提示 给出了重新运行的命令行

....

见提交 c5d2b9c

PS:顺便调整一下输出的颜色
image

oldratlee added a commit that referenced this pull request Nov 15, 2015
@liuyangc3
Copy link
Contributor Author

cool

@oldratlee oldratlee self-assigned this Nov 18, 2015
@oldratlee oldratlee assigned oldratlee and unassigned oldratlee Apr 2, 2016
@iqiancheng
Copy link

awesome !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants