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.
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).
#!/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
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 ?