0

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

pstree -pc 101
dummy(101)──dummy(102)──dummy(103)──dummy(104)──dummy(105)──dummy(106)──sleep(107)

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 ?

Update:

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)

#!/bin/bash

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

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

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 ?

12
  • 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

2

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

#!/bin/bash

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
        procs[$pid]=$ppid
    done

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

            let depth++
            pid=$ppid
        done
        if (( depth > 6 )); then
            echo $save_pid
        fi
    done
)
0

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:

/proc/12345/task/12345/children

You must log in to answer this question.

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