Search code examples
linuxstringunixsystempid

Extract PID from top in UNIX


I would like to extract PID from top for desired process and user. So, I can get a consistently updating list with this command top -u meh, but how do I extract PIDs for java for user meh from here and get the result below?

$top -u meh 

34249 meh     20   0   36.7g  11.7g   2240 S  97.0  1.5   2594:00 java
36864 meh     20   0   37.7g  12.2g   2232 S  97.0  1.6   2588:58 java
33607 meh     20   0   37.9g  12.2g   2244 S  96.7  1.6   2584:22 java
17027 meh     20   0  163228   3420   1596 R   1.0  0.0   0:00.10 top
  617 meh     20   0  117872    316    312 S   0.0  0.0   0:00.12 bash
 1626 meh     20   0  117872    236    236 S   0.0  0.0   0:00.10 bash
 2056 meh     20   0  130624    276    276 S   0.0  0.0   0:00.06 screen
 2057 meh     20   0  117892    312    308 S   0.0  0.0   0:00.08 bash
 3912 meh     20   0  117892    248    248 S   0.0  0.0   0:00.07 bash
 4516 meh     20   0  117872    236    236 S   0.0  0.0   0:00.09 bash
 7166 meh     20   0  117872    312    308 S   0.0  0.0   0:00.09 bash

result:

34249 36864 33607

Solution

  • Here is the baseline for it, you can add more grep and pipe if you want specific user

    top -p $(ps -fea|grep -i java |grep -v grep|cut -d' ' -f3 | tr '\n' ','|  head --bytes -1 )