# SccsId[] = "%W% (shell library function) %G%"
TSP_name="TERMINATE_SUBPROCESSES"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set | egrep '^$TSP_name\(\)\{$'`" != . ] && TSP_loaded=1
else # Korn or Bash shell and function already loaded?
if [ `expr "\`uname -s\`" : "[Ll][Ii][Nn][Uu][Xx]"` -eq 0 ]; then
[ ."`typeset +f|awk '/^'$TSP_name'[=\(]?/'`" != . ] && TSP_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$TSP_name'[=\(]?/'`" != . ] && TSP_loaded=1
fi
fi
if [ 0${TSP_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
TERMINATE_SUBPROCESSES() # Function documentation located at bottom. #
#----------------------------------------------------------------------#
{ [ ."${AWK}" = . ] && { { [ -x /usr/bin/nawk ] && AWK=/usr/bin/nawk; } \
|| { [ -x /bin/gawk ] && AWK=/bin/gawk ; } \
|| { [ -x /usr/bin/awk ] && AWK=/usr/bin/awk ; }; }
if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi
. $SHLIB/kill_pid.sh # Function dependencies
#------------------------------------------------------------#
# If the following variables are not set, use these defaults.#
#------------------------------------------------------------#
: ${script_name:=`basename $0`}
: ${name_root:=`echo $script_name|$AWK '{sub(/^\.+/,"");sub(/\..*/,"");print}'`}
if [ ."$log" = . ]; then
log=$name_root.log
[ ."$teelog" = . ] && teelog="cat"
fi
TSP_ptree="/usr/proc/bin/ptree" # Linux doesn't have this. Dang!
if [ -f $TSP_ptree ]; then
TSP_pids=`$TSP_ptree $$ \
| $AWK -v ppid="$$" \
'BEGIN \
{
parent_found = 0
pids = ""
}
{
if (parent_found == 1 && $1 ~ /[0-9]+/)
{
#-------------------------------------------------#
# Brackets allow us to look for our PID in a ps #
# without having to exclude awk from the search. #
#-------------------------------------------------#
sub(/^./,"[&]",$1)
pids = pids" "$1
}
else if ($1 == ppid)
parent_found = 1
}
END \
{
gsub(/^ */,"",pids)
print pids
}'`
for TSP_pid in $TSP_pids
do
TSP_ps=`ps -u $LOGNAME | awk '/'"$TSP_pid"'/ {print $1}'`
if [ ."$TSP_ps" != . ]; then
echo "`date '+%Y-%m-%d %T'`" \
"KILL_PID $TSP_ps" | $teelog
KILL_PID $TSP_ps
sleep 5
fi
done
else
echo "`date '+%Y-%m-%d %T'`" \
"No '$TSP_ptree' found for $TSP_name" | $teelog
fi # if [ -f $TSP_ptree_cmd ]; then
return 0
} # "TSP_" prefix identifies this function's local variables.
fi
#======================================================================#
# D O C U M E N T A T I O N #
#======================================================================#
# #
# Author: Bob Orlando #
# #
# Date: July 13, 2000 #
# #
# Program ID: terminate_subprocesses.sh #
# #
# Usage: TERMINATE_SUBPROCESSES <no options or arguments> #
# #
# Purpose: Terminate all the current process' subprocesses #
# (previously ABEND, but the new name better reflects #
# the function's purpose). #
# #
# Description: Normally a part of operations performed when the #
# running process is terminated (via CTRL-C or kill #
# is terminated (via CTRL-C or kill command), this #
# function uses the ptree command to attempt to locate #
# and kill all child processes before returning to the #
# caller. This process DOES NOT terminate the current #
# process; that task is left to the process itself #
# (since it may have other operations to perform #
# before exiting). #
# #
# Globals: No global variables assigned from this function. #
# "TSP_" prefix identifies local function variables. #
# #
# Calls: KILL_PID library function. #
# #
# Returns: Always returns zero (success). #
# #
# Notes: ..................................................... #
# ..................................................... #
# #
# Modified: 20yy-mm-dd Bob Orlando #
# v1.2 * .......................................... #
# .......................................... #
# * .......................................... #
# .......................................... #
# #
# 2004-08-26 Bob Orlando #
# v1.1 * Initial release. #
# #
# #
#----------------------------------------------------------------------#
|