我正在阅读ps手册页,有一些我不明白的东西.
-f does full-format listing. This option can be combined with many other
UNIX-style options to add additional columns. It also causes the
command arguments to be printed. When used with -L, the NLWP (number of
threads) and LWP (thread ID) columns will be added. See the c option,
the format keyword args, and the format keyword comm.
我只想将sid添加到ps -f的输出中
但我对以下输出感到困惑,我的理解是-o应该只添加sid到-f的输出但看起来像-o覆盖-f完全.
我知道我可以指定我想要的所有选项,如pid,ppid,pgid,sid,user,comm等等,但是有没有办法在-f的输出中添加一个额外的列?这就是手册页所说的,对吗?
[njia@cst-cgxfile01 ~]$ps -f
UID PID PPID C STIME TTY TIME CMD
njia 3397 26106 0 12:23 pts/1 00:00:00 ps -f
njia 26106 26105 0 09:45 pts/1 00:00:00 -bash
[njia@ ~]$ps -f -o sid
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
SID
26106
26106
25875
25875
25875
25875
25875
[njia@ ~]$
另外,ps -ef -o sid会将进程选择限制为拥有我的人,这也是一个惊喜.
解决方法:
要回答问题的第一部分,可以添加几个标志到-f.它们包括-l,-j,-m和-L.不幸的是-o< format>不能与-f结合使用.
实际上,获得您想要的最佳方法是准确指定您想要的内容,例如:
ps -e -o pid,ppid,pgid,sid,user,comm
但是你可以通过将-j添加到-f来实现ps -efj.这会添加PGID和SID列.
在没有-e标志的情况下演示以缩短输出,比较:
$ps -f
UID PID PPID C STIME TTY TIME CMD
myuser 123 4513 0 18:20 pts/26 00:00:00 zsh
myuser 1282 123 0 18:20 pts/26 00:00:00 ps -f
$ps -fj
UID PID PPID PGID SID C STIME TTY TIME CMD
myuser 123 4513 123 123 0 18:20 pts/26 00:00:00 zsh
myuser 1402 123 1402 123 0 18:20 pts/26 00:00:00 ps -fj
为了回答你问题的第二部分,ps -ef -o sid只显示你自己的进程的原因是它在决定你的标志不符合POSIX时切换到BSD模式.这由消息指示
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
所以它相当于运行ps ef o sid.
在BSD模式中,e表示它将打印进程的环境,f表示“森林”.并且BSD模式默认打印当前用户拥有的具有任何终端的所有进程,而不仅仅是当前终端上的进程.
尝试将-o sid更改为-o sid,cmd以查看e和f选项的效果.
$ps ef o sid
SID
12345
567
567
...
$ps ef o sid,cmd
SID CMD
12345 -zsh USER=... LOGNAME=...
567 zsh PWD=... LANG=...
567 \_ ps ef o sid,cmd LANG=... PWD=...
...
并且与ps u比较,看到显示的过程是相同的(为了简洁,我添加了| wc -l).
$ps ef o cmd | wc -l
20
$ps u | wc -l
20
标签:linux,ps
来源: https://codeday.me/bug/20190816/1667408.html