LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 07-28-2014 at 11:39 AM 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.

killtree - Creates a list of processes based from a parent process ID first then sends the signal to all of them synchronously.

killtree2 - Same as killtree but it doesn't create a list first. It immediately kills processes as it goes through every level.

killtree3 - This version kills child processes first before the parent.

killchildren, killchildren2, killchildren3 - These functions are similar to the killtree functions except that the first parent process is not killed.

listtree - Creates a list of process IDs in a process tree.
listchildren - Also creates a list of process IDs in a process tree but excludes the first parent process.

Code:
# killtree (pid, [signal = SIGTERM])
#
# Creates a list of processes first then sends the signal to all of
# them synchronously.
#
function killtree {
	listtree "$1"
	kill -s "${2:-SIGTERM}" "${LIST[@]}"
}


# killtree2 (pid, [signal = SIGTERM])
#
# This version kills processes as it goes.
#
function killtree2 {
	killtree2_ "$1" "${2:-SIGTERM}"
}


# killtree2_ (pid, signal)
#
function killtree2_ {
	local -a LIST=()
	read -a LIST -d '' < <(exec pgrep -P "$1")
	kill -s "$2" "$1"
	if [[ ${#LIST[@]} -gt 0 ]]; then
		local A
		for A in "${LIST[@]}"; do
			killtree2_ "$A" "$2"
		done
	fi
}


# killtree3 (pid, [signal = SIGTERM])
#
# This version kills child processes first before the parent.
#
function killtree3 {
	killtree3_ "$1" "${2:-SIGTERM}"
}


# killtree3_ (pid, signal)
#
function killtree3_ {
	local -a LIST=()
	read -a LIST -d '' < <(exec pgrep -P "$1")
	if [[ ${#LIST[@]} -gt 0 ]]; then
		local A
		for A in "${LIST[@]}"; do
			killtree3_ "$A" "$2"
		done
	fi
	kill -s "$2" "$1"
}


# killchildren (pid, [signal = SIGTERM])
#
# Creates a list of child processes first then sends the signal to all
# of them synchronously.
#
function killchildren {
	listchildren "$1"
	kill -s "${2:-SIGTERM}" "${LIST[@]}"
}


# killchildren2 (pid, [signal = SIGTERM])
#
# This version kills processes as it goes.
#
function killchildren2 {
	local -a LIST=()
	read -a LIST -d '' < <(exec pgrep -P "$1")
	local A SIGNAL="${2:-SIGTERM}"
	for A in "${LIST[@]}"; do
		killtree2_ "$A" "$SIGNAL"
	done
}


# killchildren3 (pid, [signal = SIGTERM])
#
# This version kills child processes first before the parent.
#
function killchildren3 {
	local -a LIST=()
	read -a LIST -d '' < <(exec pgrep -P "$1")
	local A SIGNAL="${2:-SIGTERM}"
	for A in "${LIST[@]}"; do
		killtree3_ "$A" "$SIGNAL"
	done
}


# listtree (pid)
#
# Saves list of found PIDs to array variable LIST.
#
function listtree {
	LIST=("$1")
	listchildren_ "$1"
}


# listchildren (pid)
#
# Saves list of found PIDs to array variable LIST.
#
function listchildren {
	LIST=()
	listchildren_ "$1"
}


# listchildren_ (pid)
#
function listchildren_ {
	local -a ADD=()
	read -a ADD -d '' < <(exec pgrep -P "$1")
	if [[ ${#LIST[@]} -gt 0 ]]; then
		local A I=${#LIST[@]}
		for A in "${ADD[@]}"; do
			LIST[I++]=$A
			listchildren_ "$A"
		done
	fi
}
Posted in Howtos
Views 1682 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 03:31 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration