2

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?

ctrl-alt-delor
  • 149
  • 1
  • 13
dirtside
  • 1,551
  • 5
  • 17
  • 22

4 Answers4

3

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

edit

To see only your processes

  ps -fjH -u myname
Déjà vu
  • 5,546
  • 9
  • 36
  • 55
2

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

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

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

ps uxwwf -p $(pids username)
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Close enough; this inspired me to write my own (faster ;-)) version in PHP. – dirtside Sep 24 '10 at 21:19
  • 1
    @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
0

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

ps --user dirtside -uxxf

Goladus
  • 906
  • 7
  • 6
0

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

ps_backtrace.sh