我正在閱讀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