ps: Clean way to only get parent processes?

Posted by shkschneider on Stack Overflow See other posts from Stack Overflow or by shkschneider
Published on 2012-09-11T15:36:53Z Indexed on 2012/09/11 15:37 UTC
Read the original article Hit count: 209

Filed under:
|
|
|
|

I use ps ef and ps rf a lot.

Here is a sample output for ps rf:

  PID TTY      STAT   TIME COMMAND
 3476 pts/0    S      0:00 su ...
 3477 pts/0    S      0:02  \_ bash
 8062 pts/0    T      1:16      \_ emacs -nw ...
15733 pts/0    R+     0:00      \_ ps xf
15237 ?        S      0:00 uwsgi ...
15293 ?        S      0:00  \_ uwsgi ...
15294 ?        S      0:00  \_ uwsgi ...

And today I needed to retrieve only the master process of uwsgi in a script (so I want only 15237 but not 15293 nor 15294).

As of today, I tried some ps rf | grep -v ' \\_ '... but I would like a cleaner way.

I also came accross another solution from unix.com's forums:

ps xf | sed '1d' | while read pid tty stat time command ; do [ -n "$(echo $command | egrep '^uwsgi')" ] && echo $pid ; done

But still a lot of pipes and ugly tricks.

Is there really no ps option or cleaner tricks (maybe using awk) to accomplish that?

© Stack Overflow or respective owner

Related posts about linux

Related posts about bash