New Killchilds function

This commit is contained in:
deajan 2015-09-18 09:41:18 +02:00
parent b03d436e56
commit 2415a25626
2 changed files with 41 additions and 30 deletions

View File

@ -1,3 +1,9 @@
NOTES FOR MY FUTURE SELF
------------------------
Have this kind of info written to state dir.
inotifywait -m -r -e moved_from -e moved_to -e delete /home/git/osync/dir2/
KNOWN ISSUES KNOWN ISSUES
------------ ------------
@ -9,8 +15,8 @@ RECENT CHANGES
- Added (much) more verbose debugging (and possibility to remove debug code to gain speed) - Added (much) more verbose debugging (and possibility to remove debug code to gain speed)
- Force tree function to overwrite earlier tree files - Force tree function to overwrite earlier tree files
! Add Logger DEBUG to all eval statements ! Add Logger DEBUG to all eval statements
! Unlocking should only happen after TrapQuit has successfully killed any child processes - Unlocking happens after TrapQuit has successfully killed any child processes
!- Replace child_pid by $? directly, add a better sub process killer in TrapQuit - Replace child_pid by $? directly, add a better sub process killer in TrapQuit
- Refactor [local master, local slave, remote slave] code to [local, remote][initiator, target]code - Refactor [local master, local slave, remote slave] code to [local, remote][initiator, target]code
- Added some automatic typo checks in code, for _DEBUG mode (and _PARANOIA_DEBUG now) - Added some automatic typo checks in code, for _DEBUG mode (and _PARANOIA_DEBUG now)
- Improved Logging - Improved Logging

View File

@ -4,7 +4,7 @@ PROGRAM="Osync" # Rsync based two way sync engine with fault tolerance
AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong" AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong"
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr" CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
PROGRAM_VERSION=1.1-unstable PROGRAM_VERSION=1.1-unstable
PROGRAM_BUILD=2015091401 PROGRAM_BUILD=2015091801
## 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; then if ! type -p "$BASH" > /dev/null; then
@ -107,6 +107,22 @@ function Logger {
fi fi
} }
# Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X
function KillChilds {
local pid="${1}"
local self="${2:-false}"
if children="$(pgrep -P "$pid")"; then
for child in $children; do
KillChilds "$child" true
done
fi
if [ "$self" == true ]; then
kill -s SIGTERM "$pid" || (sleep 10 && kill -9 "$pid" &)
fi
}
function TrapError { function TrapError {
local job="$0" local job="$0"
local line="$1" local line="$1"
@ -149,34 +165,23 @@ function TrapQuit {
exitcode=0 exitcode=0
fi fi
#TODO: Replace the following basic code with some code that kills all child processes (this code only kills the current child pid it's aware of via WaitFor(Task)Completion #TODO: Check new KillChilds function for service mode
if ps -p $CHILD_PID > /dev/null 2>&1
then
kill -s SIGTERM $CHILD_PID
if [ $? == 0 ]; then
Logger "Stopped child process [$CHILD_PID]." "DEBUG"
else
Logger "Could not terminate child process [$CHILD_PID]. Trying the hard way." "DEBUG"
kill -9 $CHILD_PID
if [ $? != 0 ]; then
Logger "Could not kill child process [$CHILD_PID]." "ERROR"
fi
fi
fi
if ps -p $OSYNC_SUB_PID > /dev/null 2>&1 # if ps -p $OSYNC_SUB_PID > /dev/null 2>&1
then # then
kill -s SIGTERM $OSYNC_SUB_PID # kill -s SIGTERM $OSYNC_SUB_PID
if [ $? == 0 ]; then # if [ $? == 0 ]; then
Logger "Stopped sub process [$OSYNC_SUB_PID]." "DEBUG" # Logger "Stopped sub process [$OSYNC_SUB_PID]." "DEBUG"
else # else
Logger "Could not terminate sub process [$OSYNC_SUB_PID]. Trying the hard way." "DEBUG" # Logger "Could not terminate sub process [$OSYNC_SUB_PID]. Trying the hard way." "DEBUG"
kill -9 $OSYNC_SUB_PID # kill -9 $OSYNC_SUB_PID
if [ $? != 0 ]; then # if [ $? != 0 ]; then
Logger "Could not kill sub process [$OSYNC_SUB_PID]." "ERROR" # Logger "Could not kill sub process [$OSYNC_SUB_PID]." "ERROR"
fi # fi
fi # fi
fi # fi
KillChilds $$ > /dev/null 2&>1
exit $exitcode exit $exitcode
} }