2017-09-01 43 views
2

我想从我的流程中获得pid。我做ps aux | cut -d ' ' -f 2但我注意到,有时它得到pid和有时没有:如何总是从`ps aux`命令切断PID?

[[email protected] ~]$ 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 
[[email protected] ~]$ ps aux | cut -d ' ' -f 2 

12290 
[[email protected] ~]$ ps aux | cut -d ' ' -f 3 
2049 

通知第一cut命令其输送至2而第二个是它管道到3。如何从这些中挑选出PID而无需知道使用哪个号码(23)?

有人可以告诉我这些以及为什么它拿起一个而不是其他的?

回答

4

-d ' '表示使用单个空格作为分隔符。由于2049之前有1个空间,12290之前有2个空间,所以你的命令通过-f 2-f 3得到它们。

我推荐使用ps aux | awk '{print $2}'来获得这些pid。

或者您可以使用tr挤进第一 ps aux | tr -s ' ' | cut -d ' ' -f 2

3

您可以使用该选项-o只打印PID那些空间:

ps -u user -o pid