views:

26

answers:

1

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)

A: 

Here is a slightly shorter and possibly slightly faster version of yours. It probably relies on some GNU-specific features:

ps ufww --sid=$(ps -C apache2 -o sess= | sort -u | grep -v -E '^ +0$' | tr $'\n' ',' | sed 's/,$/\n/; s/ //g')

Over 50 characters shorter.

Shorter yet and with no convoluted machinations:

ps -C apache2 fww -o user,pid,%cpu,%mem,vsz,rss,tty,stat,start,time,cmd

Look! No scrollbar!

I don't understand what you mean by:

For example I would actually want to have included all childs relative any parent.

Isn't that what ps auxfww does?

If you want to easily specify a process name as a parameter, you could create a ps "family" function:

psf () { ps -C $1 fww -o user,pid,%cpu,%mem,vsz,rss,tty,stat,start,time,cmd; }
Dennis Williamson