Writing a script that takes PID as an argument and prints PID's of all its GRANDchildren and grandgrandchildren

Solution 1:

Here's a meta-language design(not implemented,not tested, must be assumed to not work). Actual coding is left as an exercise for the student.

In a bashscript (beginning with #!/bin/bash):

Define a ppid function taking a PID as a parameter, and returning the PID's parent.

Define a children function taking a PID as a parameter, and returning a space-seperated list ("pid pid pid " note the trailing space) of PIDs that have the parameter PID as a parent. Check ALL the PIDs on the system, using the ppid function.

startPID=$1
childlist="$(children $startPID)"
gchildlist=""
ggchildlist=""
for kid in $childlist ; do
    gchildlist="$gchildlist $(children $kid)"
done
for kid in $gchildlist ; do
    ggchildlist="$ggchildlist $(children $kid)"
done
echo "Grand :  $gchildlist"
echo "Ggrand: $ggchildlist"