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

执行脚本的用户名超过8个字符时无法执行脚本 #62

Merged
merged 1 commit into from
Nov 1, 2017

Conversation

xiongchen2012
Copy link
Contributor

@xiongchen2012 xiongchen2012 commented Nov 1, 2017

ps -o user参数,当用户超过8个时,超过部分会显示为+号。例如:whoami=njiclscy-7

ps -Leo pid,lwp,user,comm,pcpu --no-headers
19317 19649 **njiclsc+** java             0.0

执行脚本提示:

[1] Fail to jstack Busy(0.2%) thread(19826/0x4d72) stack of java process(19317) under user(njiclsc+).
User of java process(njiclsc+) is not current user(njiclscy-7), need sudo to run again:

这个问题导致脚本无法执行。但实际上启动Java进程的用户确实是njiclscy-7

PR的修改方法是取出当前用户名,使用ruser选项输出用户名。

@oldratlee
Copy link
Owner

oldratlee commented Nov 1, 2017

👍 问题收到 @xiongchen2012

我看看你实现的解法

@oldratlee
Copy link
Owner

oldratlee commented Nov 1, 2017

问题测试分析

找了一台机器测试超过8个字符的用户名:

$ ps -eo comm,user,ruser,uid
COMMAND         USER     RUSER      UID
init            root     root         0
sshd            root     root         0
crond           root     root         0
...
java            admin    admin      662
...
bash            59504    59504    59504
ps              59504    59504    59504
...

$ ps -eo comm,user -o ruser=${USER} -o uid
COMMAND         USER     username_long   UID
init            root     root            0
sshd            root     root            0
crond           root     root            0
java            admin    admin           662
bash            59504    username_long   59504
ps              59504    username_long   59504

解释说明:

  • 第一条命令可以看到,userruser,都会有截断的问题。
    在我的机器上是截断成uid
  • 第二条命令,是因为ruser指定了列标题导致列变宽了,所以没有被截断。
- ps -Leo pid,lwp,user,comm,pcpu --no-headers | {		 
+ ps -Leo pid,lwp -o ruser=${USER} -o comm,pcpu --no-headers | {

按你上面的修改,如果有个用户比 你当前用户名 还长的话,还是会有截断的bug。
# 这个脚本可以去jstack别人的java进程(如以sudo方式运行)

解法

确定了是列宽导致截断的问题,看了一下psman,是可以显式指定列宽的:

-o format
       User-defined format.  format is a single argument in the form of a
       blank-separated or comma-separated list, which offers a way to specify
       individual output columns.  The recognized keywords are described in the
       STANDARD FORMAT SPECIFIERS section below.  Headers may be renamed (ps -o
       pid,ruser=RealUser -o comm=Command) as desired.  If all column headers are
       empty (ps -o pid= -o comm=) then the header line will not be output.
       Column width will increase as needed for wide headers; this may be used to
       widen up columns such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm).
       Explicit width control (ps opid,wchan:42,cmd) is offered too.
       ...

即这一句: Explicit width control (ps opid,wchan:42,cmd) is offered too.

验证

$ ps -eo comm,user:20,ruser,uid
COMMAND         USER                 RUSER      UID
init            root                 root         0
sshd            root                 root         0
crond           root                 root         0
...
java            admin                admin      662
...
bash            username_long        59504    59504
ps              username_long        59504    59504
...

实现修改

- ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
+ ps -Leo pid,lwp,user:80,comm,pcpu --no-headers | {

假定没有超过80字符的用户名了,这个工程实现上实际上足够了 😸

Done! @xiongchen2012

@oldratlee oldratlee merged commit 3de6dcc into oldratlee:master Nov 1, 2017
oldratlee added a commit that referenced this pull request Nov 1, 2017
@oldratlee
Copy link
Owner

oldratlee commented Nov 1, 2017

@xiongchen2012 PR已经合并

另外,实现优化 也提交了, 见 f817da7

@xiongchen2012 麻烦测试一下,在你的环境是不是也是OK的?? 💓

❤️ Thanks for your bug submission and implementation commit! ❤️

oldratlee pushed a commit that referenced this pull request Nov 1, 2017
Merge pull request #62 from xiongchen2012/master
oldratlee added a commit that referenced this pull request Nov 1, 2017
oldratlee added a commit that referenced this pull request Nov 1, 2017
oldratlee added a commit that referenced this pull request Nov 1, 2017
oldratlee added a commit that referenced this pull request Nov 1, 2017
@xiongchen2012
Copy link
Contributor Author

Nice work~
man果然还是最好的文档

@oldratlee
Copy link
Owner

@xiongchen2012 方便在你的环境测试一下?结果如何?

@oldratlee oldratlee added the bug label Nov 1, 2017
@xiongchen2012
Copy link
Contributor Author

已经OK了,谢谢。

@oldratlee
Copy link
Owner

oldratlee commented Nov 2, 2017

@oldratlee
Copy link
Owner

oldratlee commented Nov 13, 2017

@xiongchen2012

修改了实现,没有username 80个字符的限制了

修改参见 808a2e3

  • 使用了 -ww选项,不限宽度
  • user列放到最后一列

@oldratlee oldratlee self-assigned this Nov 13, 2017
oldratlee pushed a commit that referenced this pull request Nov 28, 2017
Merge pull request #62 from xiongchen2012/master
oldratlee added a commit that referenced this pull request Nov 28, 2017
oldratlee added a commit that referenced this pull request Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants