Grep 如何始终从“ps aux”命令中剪切PID?
我想从我的进程中得到pid。我确实Grep 如何始终从“ps aux”命令中剪切PID?,grep,cut,ps,Grep,Cut,Ps,我想从我的进程中得到pid。我确实ps aux | cut-d'-f2,但我注意到有时它会得到pid,有时它不会: [user@ip ~]$ ps aux user 2049 0.5 10.4 6059216 1623520 ? Sl date 8:48 process user 12290 0.3 6.9 5881568 1086244 ? Sl date 2:30 [user@ip ~]$ ps aux | cut -d ' ' -f 2 12
ps aux | cut-d'-f2
,但我注意到有时它会得到pid,有时它不会:
[user@ip ~]$ ps aux
user 2049 0.5 10.4 6059216 1623520 ? Sl date 8:48 process
user 12290 0.3 6.9 5881568 1086244 ? Sl date 2:30
[user@ip ~]$ ps aux | cut -d ' ' -f 2
12290
[user@ip ~]$ ps aux | cut -d ' ' -f 3
2049
请注意,第一个cut
命令是将其管道化到2
,而第二个命令是将其管道化到3
。如何在不知道使用哪个数字(2
或3
)的情况下从中选择PID
有人能告诉我这两者的区别吗?为什么它会选择一个而不是另一个
-d'
表示使用单个空格作为分隔符。因为2049之前有1个空格,12290之前有2个空格,所以您的命令通过-f2
和-f3
获取它们
我建议使用ps aux|awk'{print$2}'
来获取这些PID
或者您可以先使用tr
压缩这些空格
ps aux | tr-s'| cut-d'-f2
您可以使用选项-o仅打印pid:
ps -u user -o pid
始终可以使用pgrep获取进程的PID 例如,带有PS AUX的PID
wix@wsys:~$ ps aux | grep sshd
root 1101 0.0 0.0 72304 3188 ? Ss Oct14 0:00 /usr/sbin/sshd -D
root 6372 0.0 0.1 105692 7064 ? Ss 06:01 0:00 sshd: wix [priv]
wix 6481 0.0 0.1 107988 5748 ? S 06:01 0:00 sshd: wix@pts/1
root 6497 0.0 0.1 105692 7092 ? Ss 06:01 0:00 sshd: wix [priv]
wix 6580 0.0 0.1 107988 5484 ? S 06:01 0:00 sshd: wix@pts/2
wix 6726 0.0 0.0 13136 1044 pts/1 S+ 06:12 0:00 grep --color=auto sshd
现在只需pgrep获取PID
wix@wsys:~$ pgrep sshd
1101
6372
6481
6497
6580
wix@wsys:~$
这与OP的问题有什么关系?我想目标是获得PID,所以为什么不使用手头的工具,而不是使用复杂的命令链。他没有说明他的最终意图,但由于收集所有进程的PID似乎毫无意义,我想你可能是对的。即使你想要所有进程,你也可以做pgrep“”如何处理
/sbin/lvmetad-f
pgrep将不显示PID