KillChild function works on Linux,bsd,macos

This commit is contained in:
deajan 2015-09-15 12:56:41 +02:00
parent 62600fff39
commit 1fa695e3b0
1 changed files with 15 additions and 14 deletions

View File

@ -5,7 +5,7 @@
AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong" AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong"
CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr"
PROGRAM_VERSION=1.9pre PROGRAM_VERSION=1.9pre
PROGRAM_BUILD=2015091401 PROGRAM_BUILD=2015091501
## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode ## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode
if ! type -p "$BASH" > /dev/null if ! type -p "$BASH" > /dev/null
@ -110,21 +110,22 @@ function LogDebug
fi fi
} }
# Highly portable killtree function from http://stackoverflow.com/a/18214698/2635443, modded not to kill itself # Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X, see http://stackoverflow.com/a/32566506/2635443
function KillTree { KillChilds() {
local parent=$1 child local pid="${1}"
local self="${2:-false}"
for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do if children="$(pgrep -P "$pid")"; then
KillTree $child for child in $children; do
done KillChilds "$child" true
done
if [ $parent -ne $$ ]; then fi
kill -9 $parent > /dev/null 2>&1
fi if [ "$self" == true ]; then
kill -s SIGTERM "$pid" || (sleep 10 && kill -9 "$pid" &)
fi
} }
killtree $$
function TrapError function TrapError
{ {
local JOB="$0" local JOB="$0"
@ -173,7 +174,7 @@ function TrapQuit
Log "Backup script finshed." Log "Backup script finshed."
fi fi
KillTree $$ KillChilds $$ > /dev/null 2&>1
} }
function Spinner function Spinner