LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > konsolebox
User Name
Password

Notices


Rate this Entry

Bash functions to list and kill or send signals to process trees

Posted 03-31-2012 at 11:54 PM by konsolebox
Updated 05-27-2018 at 03:11 PM by konsolebox

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.

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
}
The latest versions of these functions are updated here: https://github.com/konsolebox/script.../killtree.bash
Posted in Howtos
Views 4304 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration