Skip to content

Instantly share code, notes, and snippets.

@tangnotes
Last active September 5, 2017 15:20
Show Gist options
  • Save tangnotes/b1d37095506dbd12437f920746c123f9 to your computer and use it in GitHub Desktop.
Save tangnotes/b1d37095506dbd12437f920746c123f9 to your computer and use it in GitHub Desktop.
Get all child PIDs recursively
#!/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