From 62600fff39c69c5cbd7565990462fac86741daca Mon Sep 17 00:00:00 2001 From: deajan Date: Mon, 14 Sep 2015 11:20:01 +0200 Subject: [PATCH] Added new killtree function --- CHANGELOG.md | 1 + obackup.sh | 53 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fcf478..13aa011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ UNDER WORK CHANGELOG --------- +- New function to kill child processes - Fixed no_maxtime not honored - Improved some logging, also added highlighting to stdout errors - Backported some fixes from Osync diff --git a/obackup.sh b/obackup.sh index c5706e1..d6df1f3 100755 --- a/obackup.sh +++ b/obackup.sh @@ -5,7 +5,7 @@ AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong" CONTACT="http://www.netpower.fr/obackup - ozy@netpower.fr" PROGRAM_VERSION=1.9pre -PROGRAM_BUILD=2015090603 +PROGRAM_BUILD=2015091401 ## 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 @@ -110,6 +110,21 @@ function LogDebug fi } +# Highly portable killtree function from http://stackoverflow.com/a/18214698/2635443, modded not to kill itself +function KillTree { + local parent=$1 child + + for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do + KillTree $child + done + + if [ $parent -ne $$ ]; then + kill -9 $parent > /dev/null 2>&1 + fi +} + +killtree $$ + function TrapError { local JOB="$0" @@ -130,35 +145,35 @@ function TrapStop function TrapQuit { # Kill all child processes - if type -p pkill > /dev/null 2>&1 - then - ## Added || : to return success even if there is no child process to kill - pkill -TERM -P $$ || : - elif [ "$LOCAL_OS" == "msys" ] || [ "$OSTYPE" == "msys" ] - then + #if type -p pkill > /dev/null 2>&1 + #then + # ## Added || : to return success even if there is no child process to kill + # pkill -TERM -P $$ || : + #elif [ "$LOCAL_OS" == "msys" ] || [ "$OSTYPE" == "msys" ] + #then ## This is not really a clean way to get child process pids, especially the tail -n +2 which resolves a strange char apparition in msys bash - for pid in $(ps -a | awk '{$1=$1}$1' | awk '{print $1" "$2}' | grep " $$$" | awk '{print $1}' | tail -n +2) - do - kill -9 $pid > /dev/null 2>&1 - done - else - for pid in $(ps -a --Group $$) - do - kill -9 $pid - done - fi + # for pid in $(ps -a | awk '{$1=$1}$1' | awk '{print $1" "$2}' | grep " $$$" | awk '{print $1}' | tail -n +2) + # do + # kill -9 $pid > /dev/null 2>&1 + # done + #else + # for pid in $(ps -a --Group $$) + # do + # kill -9 $pid + # done + #fi if [ $error_alert -ne 0 ] then SendAlert CleanUp LogError "Backup script finished with errors." - exit 1 else CleanUp Log "Backup script finshed." - exit 0 fi + + KillTree $$ } function Spinner