Last active
October 6, 2015 16:22
-
-
Save jmiserez/431efc13fc6ca1190281 to your computer and use it in GitHub Desktop.
Find child processes (and their children, etc.) with one ps call (no race conditions)
This file contains 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 | |
# Original idea: http://superuser.com/a/784102/59125 | |
pidtree(){ | |
pids_for_ppid=() | |
while read pid ppid; do | |
pids_for_ppid[$ppid]+=" $pid" | |
done < <(ps -e -o pid,ppid --no-headers) | |
print_children(){ | |
for i in ${pids_for_ppid[$1]}; do | |
( (print_children $i) ) | |
echo $i | |
done | |
} | |
( (print_children $1) ) | |
echo $1 | |
} | |
pidtree $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment