LinuxQuestions.org
Visit Jeremy's Blog.
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 04-11-2013 at 07:47 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.

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:
#!/bin/bash


# void killtree (uint <parent_pid>, [string <signal>])
#
# Creates a list of processes first then sends the signal to all of
# them synchronously.
#
function killtree {
	listtree "$1"
	kill -s "${2:-SIGTERM}" "${__A0[@]}"
}


# void killtree2 (uint <parent_pid>, [string <signal>])
#
# This version kills processes as it goes.
#
function killtree2 {
	killtree2_ "$1" "${2:-SIGTERM}"
}


# void killtree2_ (uint <pid>, string 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
}


# void killtree3 (uint <parent_pid>, [string <signal>])
#
# This version kills child processes first before the parent.
#
function killtree3 {
	killtree3_ "$1" "${2:-SIGTERM}"
}


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


# void killchildren (uint <parent_pid>, [string <signal>])
#
# Creates a list of child processes first then sends the signal to all
# of them synchronously.
#
function killchildren {
	listchildren "$1"
	kill -s "${2:-SIGTERM}" "${__A0[@]}"
}


# void killchildren2 (uint <parent_pid>, [string <signal>])
#
# 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
}


# void killchildren3 (uint <parent_pid>, [string <signal>])
#
# 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
}


# void listtree (uint <pid>)
#
# > uint array __A0 = list of PIDs
#
function listtree {
	__A0=("$1")
	listchildren_ "$1"
}


# void listchildren (uint <pid>)
#
# > uint array __A0 = list of PIDs
#
function listchildren {
	__A0=()
	listchildren_ "$1"
}


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

Comments

 

  



All times are GMT -5. The time now is 06:38 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