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?

4 Answers 4

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)
2
  • Close enough; this inspired me to write my own (faster ;-)) version in PHP.
    – dirtside
    Sep 24, 2010 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. Sep 24, 2010 at 21:23
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
4
  • That shows processes the OP wants excluded. Sep 23, 2010 at 23:00
  • Updated the answer
    – Déjà vu
    Sep 23, 2010 at 23:29
  • That omits processes the OP wants shown. Sep 24, 2010 at 2:30
  • @DennisWilliamson Well..
    – Déjà vu
    Aug 25, 2022 at 4:20
0

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

ps --user dirtside -uxxf

1
  • 1
    That doesn't show processes the OP wants included. Sep 23, 2010 at 23:01
0

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

ps_backtrace.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .