Posted By

XtreamIT on 12/20/12


Tagged


Versions (?)

Kill processtree


 / Published in: Bash
 

Kill process tree

  1. #!/bin/bash
  2.  
  3. killtree() {
  4. local _pid=$1
  5. local _sig=${2-TERM}
  6. kill -stop ${_pid} # needed to stop quickly forking parent from producing child between child killing and parent killing
  7. for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
  8. killtree ${_child} ${_sig}
  9. done
  10. kill -${_sig} ${_pid}
  11. }
  12.  
  13. if [ $# -eq 0 -o $# -gt 2 ]; then
  14. echo "Usage: $(basename $0) <pid> [signal]"
  15. exit 1
  16. fi
  17.  
  18. killtree $@

Report this snippet  

You need to login to post a comment.