Last active
September 5, 2017 15:20
-
-
Save tangnotes/b1d37095506dbd12437f920746c123f9 to your computer and use it in GitHub Desktop.
Get all child PIDs recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ppids=$1 | |
if [ "x$ppids" == "x" ] | |
then | |
echo "Usage: $0 PPID" | |
exit 1 | |
fi | |
function getChildPids() { | |
ppid=$1 | |
echo `ps --no-headers -o pid --ppid $ppid` | |
} | |
level=1 | |
pids="" | |
while [ "x$ppids" != "x" ] | |
do | |
next_ppids="" | |
for ppid in $ppids | |
do | |
i=0 | |
while [ $i -lt $level ] | |
do | |
echo -n ">" >&2 | |
i=$((i+1)) | |
done | |
echo " $ppid" >&2 | |
pids="$pids,$ppid" | |
childs=`getChildPids $ppid` | |
next_ppids="$childs $next_ppids" | |
done | |
level=$((level+1)) | |
ppids=$next_ppids | |
done | |
echo "ALL PIDS: $pids" >&2 | |
echo ${pids:1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment