23

Command pstree PID can show all subprocess information of the process specified by PID. However, I also want to know all parent process information of the process PID, how can I get it?

An example:

init
|- parent_process
|    `- current_process
|       |- subprocess_1
|       `- subprocess_2
`- other_process

What I want is when I run pstree current_process_pid, I want to get below output:

init
`- parent_process
    `- current_process
       |- subprocess_1
       `- subprocess_2

When I run pstree subprocess_1_pid, it will output:

init
`- parent_process
    `- current_process
       `- subprocess_1
1
  • Note: Using -l option of the pstree/ps commands shows long lines with command line arguments of the process. Helpful when you want to trace command line arguments for each process and see which command/script gets triggered (for example figure out which backend scripts are run for a web UI operation).
    – GuruM
    Commented Aug 6, 2013 at 12:50

3 Answers 3

25
# With my psmisc 22.20:
pstree -p -s PID

Maybe if with ps -ef:

awk -vPID=$1 '
function getParent ( pid ) {
    if (pid == "" || pid == "0") return;
    while ("ps -ef | grep "pid | getline) {
        if ($2 == pid) {
            print $8"("$2") Called By "$3;
            getParent($3);
            break;
        }
    }
    close ("ps -ef")
}

BEGIN { getParent(PID) }
'

This is ugly assuming ps output column and order. Actually one single run of ps -ef contains all info needed. This don't worth the time, I still recommend updating psmisc, it won't hurt.

EDIT: A mimic using single run ps -ef:

ps -ef | awk -vPID=$1 '
function getpp ( pid, pcmd, proc ) {
    for ( p in pcmd ) {
        if (p == pid) {
            getpp(proc[p], pcmd, proc);
            if (pid != PID) printf("%s(%s)───", pcmd[pid], pid);
        }
    }
}

NR > 1 {
    # pid=>cmd
    pcmd[$2] = $8;
    # pid=>Parent
    pproc[$2] = $3;
}

END {
    getpp(PID, pcmd, pproc);
    printf "\n";
    system("pstree -p "PID);
}'
3
  • 3
    -s option not supported by my pstree which installed by psmisc-22.2-7.el5_6.2 Commented Oct 15, 2012 at 4:06
  • 2
    As a mnemonic I remember the args as laps as in pstree -laps <pid> to get the branch with additional information.
    – haridsv
    Commented Sep 21, 2015 at 12:14
  • @MeaCulpa hmm for me pstree -p -s PID doesn't work, but pstree -s -p PID works... on mac
    – lnshi
    Commented Dec 4, 2018 at 4:23
10

I found laps options mentioned by @haridsv (pstree -laps <pid>) being a solution. It was a bit verbose for me though, so I'd stick to a shorter aps output.

To get the process tree of the current process (its ID is $$ in Bash):

pstree -aps $$

That prints the process tree like this:

systemd,1
  └─kitty,86739
      └─bash,86742
          └─pstree,86904 -aps 86742
1
  • (using pstree (PSmisc) 23.4) for maximum verbosity: pstree -calpsSngtuZ $SOME_PID
    – Abdull
    Commented Jul 17, 2023 at 6:46
0

One-liner Linux-only POSIX shell. Works in busybox and every other Linux system:

pl() { for f in /proc/$1/task/*/children;do l=""; [ -r $f ] &&read l<$f;for p in $l;do echo $p; pl $p; done; done; }

Usage: run sleep 10000 & then echo $(pl $$) will show you all subprocesses below this ($$) shell, which will only be the one sleep proc currently. But, if that sleep proc spawned children, then pl would recursively descend until it found all children

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