Skip to content

Instantly share code, notes, and snippets.

@jmiserez
Last active October 6, 2015 16:22
Show Gist options
  • Save jmiserez/431efc13fc6ca1190281 to your computer and use it in GitHub Desktop.
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)
#!/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