ps forrest for session id

Posted by azatoth on Stack Overflow See other posts from Stack Overflow or by azatoth
Published on 2010-06-12T00:18:18Z Indexed on 2010/06/12 0:22 UTC
Read the original article Hit count: 609

Filed under:
|
|

Often I want to get a nice readout what process are running and their relationship; I usually by habit runs ps auxfww and eventual grep for the process in question.

Having been thinking about the problem I tried to create an oneliner to get the process tree in ps ufww format for all processes which has the session id specified by arbitrary process name(s); ending up in following code:

ps ufww --sid=$(ps -C apache2 -o sess --no-headers | sort | uniq | grep -v -E '^ +0$' | awk 'NR==1{x=$0;next}NF{x=x","$0};END{gsub(/[[:space:]]*/,"",x);print x}')

giving for example following output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      4157  0.0  0.1  41264  3120 ?        Ss   Jun11   0:00 /usr/sbin/apache2 -k start
www-data  4329  0.0  0.0  41264  1976 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  4330  0.0  0.0  41264  2028 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  4331  0.0  0.0  41264  2028 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  4332  0.0  0.0  41264  2028 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  4333  0.0  0.0  41264  2032 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  6648  0.0  0.0  41264  1884 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  6654  0.0  0.0  41264  1884 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start
www-data  6655  0.0  0.0  41264  1884 ?        S    Jun11   0:00  \_ /usr/sbin/apache2 -k start

I do wonder now if anyone has an better idea to solve this issue? Are there anything out there that is easier to "oneline" and gives above or better information? For example I would actually want to have included all childs relative any parent.

(uncertain if this should be on SF instead, but felt it was more like an programming question)

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell-scripting