Bash functions to list and kill or send signals to process trees
These functions can be used in scripts or interactive shells to manipulate process trees. At least bash version 3.0 is needed. May also work with some earlier versions.
kill_tree - Creates a list of processes based from a parent process ID first then sends the signal to all of them synchronously.
kill_tree_2 - Same as killtree but it doesn't create a list first. It immediately kills processes as it goes through every level.
kill_tree_3 - This version kills child processes first before the parent.
kill_children, kill_children_2, kill_children_3 - These functions are similar to the killtree functions except that the first parent process is not killed.
list_tree - Creates a list of process IDs in a process tree.
list_children - Also creates a list of process IDs in a process tree but excludes the first parent process.
The latest versions of these functions are updated here: https://github.com/konsolebox/script.../killtree.bash
kill_tree - Creates a list of processes based from a parent process ID first then sends the signal to all of them synchronously.
kill_tree_2 - Same as killtree but it doesn't create a list first. It immediately kills processes as it goes through every level.
kill_tree_3 - This version kills child processes first before the parent.
kill_children, kill_children_2, kill_children_3 - These functions are similar to the killtree functions except that the first parent process is not killed.
list_tree - Creates a list of process IDs in a process tree.
list_children - Also creates a list of process IDs in a process tree but excludes the first parent process.
Code:
# kill_tree (pid, [signal = SIGTERM]) # # Creates a list of processes first then sends the signal to all of # them synchronously. # function kill_tree { local LIST=("$1") list_children_ "$1" kill -s "${2-SIGTERM}" "${LIST[@]}" } # kill_tree_2 (pid, [signal = SIGTERM]) # # This version kills processes as it goes. # function kill_tree_2 { local LIST=() S=${2-SIGTERM} A IFS=$'\n' read -ra LIST -d '' < <(exec pgrep -P "$1") kill -s "$S" "$1" for A in "${LIST[@]}"; do kill_tree_2 "$A" "$S" done } # kill_tree_3 (pid, [signal = SIGTERM]) # # This version kills child processes first before the parent. # function kill_tree_3 { local LIST=() S=${2-SIGTERM} A IFS=$'\n' read -ra LIST -d '' < <(exec pgrep -P "$1") for A in "${LIST[@]}"; do kill_tree_3 "$A" "$S" done kill -s "$S" "$1" } # kill_children (pid, [signal = SIGTERM]) # # Creates a list of child processes first then sends the signal to all # of them synchronously. # function kill_children { local LIST=() list_children_ "$1" kill -s "${2-SIGTERM}" "${LIST[@]}" } # kill_children_2 (pid, [signal = SIGTERM]) # # This version kills processes as it goes. # function kill_children_2 { local LIST=() S=${2-SIGTERM} A IFS=$'\n' read -ra LIST -d '' < <(exec pgrep -P "$1") for A in "${LIST[@]}"; do kill_tree_2 "$A" "$S" done } # kill_children_3 (pid, [signal = SIGTERM]) # # This version kills child processes first before the parent. # function kill_children_3 { local LIST=() S=${2-SIGTERM} A IFS=$'\n' read -ra LIST -d '' < <(exec pgrep -P "$1") for A in "${LIST[@]}"; do kill_tree_3 "$A" "$S" done } # list_tree (pid) # # Saves list of found PIDs to array variable LIST. # function list_tree { LIST=("$1") list_children_ "$1" } # list_children (pid) # # Saves list of found PIDs to array variable LIST. # function list_children { LIST=() list_children_ "$1" } # list_children_ (pid) # function list_children_ { local ADD=() A IFS=$'\n' read -ra ADD -d '' < <(exec pgrep -P "$1") LIST+=("${ADD[@]}") for A in "${ADD[@]}"; do list_children_ "$A" done }
Total Comments 0