2017-09-01 115 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