Suppose there is a process dummy.sh whose pid is 101 & has seven descendants:

pstree -pc 101

But how to fetch PID's of all the process in Bash script which has more than six(in our example dummy.sh 101) descendants. I do not want PID's of process whose descendants are less than six.

I tried PS -u myuser but it only displays all user process. But how to get PID's of user processes which have descendants greater than six ?


For example purpose I use below recursive script to trigger seven straight chain of descendants. I want to get PID 101 as it has more than six descendants.

Similarly as this dummy process is triggered by bash(100) terminal which will be parent to dummy(101) then I want Pid 100 as well (as it also meets the condition of having more than six descendants). bash(100)──dummy(101)──dummy(102).....dummy(107)


if [[ "$#" -ne 1 ]]; then
    set -- 7

if [[ "$1" -gt 2 ]]; then
    echo 'descendant process' "$1"
    "$0" "$(($1 - 1))"
    sleep 500

Criteria: I will consider only the PID's returned by ps -u $USER -o pid command. As my requirement is only to consider user preprocess. I will loop them to find if a user process has more than six descendants, but my question is how do i find descendants count for a particular PID ?

  • I've got an smbd process with three immediate descendants, i.e. not in a chain. Run this code to output the example as if from pstree - echo 'smbd(1076)─┬─cleanupd(1130)'; echo ' ├─lpqd(1147)'; echo ' └─smbd-notifyd(1126)'. In the context of your question does that count as three descendants, or one descendant three times? Feb 18, 2022 at 15:07
  • My example includes descendants of descendants like a chain. In my example dummy(101) has one child i,e dummy(102). But also dummy(101) has six descendants. Feb 18, 2022 at 15:17
  • Yes, I can see that. I'm asking about my example. Or perhaps your question relates to a chain of six descendants, rather than just six descendants? Feb 18, 2022 at 15:32
  • 1
    In your example, 101 might have a parent of 80, and in turn that process will have a parent of PID 1. Ultimately, all processes belong to a chain from 1, so the answer to your question is potentially just to list PID 1. This is a user process (as in, it's not a kernel process). Or did you mean we should only consider processes owned by the current user? Feb 18, 2022 at 16:50
  • 1
    I don't see that in your question. Please add it, as it's a really important clarification Feb 18, 2022 at 19:55

2 Answers 2


I'm sure there's a much more efficient way of doing this, but this works:


declare -A procs
ps -u $USER --no-headers -o pid,ppid | (

    # build an associate array that maps pids to parent pids
    while read pid ppid ; do

    # for each process, walk up the tree, counting processes
    for pid in "${!procs[@]}"; do
        while :; do
            [[ $ppid ]] || break

            let depth++
        if (( depth > 6 )); then
            echo $save_pid

You could also browse the /proc filesystem. For every process running there is a file called "children" where all direct descendants are listed space-separated.

For process 12345 for instance you will find the information about its child processes in:


You must log in to answer this question.

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