Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's what I want to do: I want to get a tree-formatted list of processes from ps (as when you do ps auxwwf), but only of processes that are either owned by me, or are ancestors of processes owned by me. So if I own a bash process way down the tree, and it has ancestors owned by root, I want to see those root ancestors in addition to the ones I own. I do not want to see any process trees that do not contain any processes owned by me.

Is there any way to do this with ps's normal options, or do I need to write a script to parse the output?

share|improve this question
up vote 1 down vote accepted

This won't be very fast, but it seems to do the trick:

# Bash, GNU ps
pidchain ()
    if [[ -z $1 ]]; then
    if (( $1 == 0 )); then
        echo "$1";
        pidchain $(ps -p $1 o ppid=);

pids () {
    ps o pid= -u $1 |
        while read pid
            pidchain $pid
        done |
            sort -nu

ps uxwwf -p $(pids username)
share|improve this answer
Close enough; this inspired me to write my own (faster ;-)) version in PHP. – dirtside Sep 24 '10 at 21:19
@dirtside: Would you post your code as an answer? I actually looked at the source of pstree as a potential starting point (and probably would need to look at pgrep as well), but that project will have to take a number and wait in line. – Dennis Williamson Sep 24 '10 at 21:23

Try ps -ejH that displays a tree of processes, based of father-sons links.


To see only your processes

  ps -fjH -u myname
share|improve this answer
That shows processes the OP wants excluded. – Dennis Williamson Sep 23 '10 at 23:00
Updated the answer – ringø Sep 23 '10 at 23:29
That omits processes the OP wants shown. – Dennis Williamson Sep 24 '10 at 2:30

This depends on your os, but based on the ps command you gave I think this might work:

ps --user dirtside -uxxf

share|improve this answer
That doesn't show processes the OP wants included. – Dennis Williamson Sep 23 '10 at 23:01

My take on this, using iteration instead of recursion (no worry that you'll overflow your stack):

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.