Multi pid wait and killchild functions

This commit is contained in:
deajan 2016-08-03 10:02:07 +02:00
parent 0fea93bbbf
commit 5c43179ef4
1 changed files with 47 additions and 48 deletions

View File

@ -1,4 +1,4 @@
## FUNC_BUILD=2016080203 ## FUNC_BUILD=2016080204
## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode
@ -38,7 +38,7 @@ fi #__WITH_PARANOIA_DEBUG
## allow debugging from command line with _DEBUG=yes ## allow debugging from command line with _DEBUG=yes
if [ ! "$_DEBUG" == "yes" ]; then if [ ! "$_DEBUG" == "yes" ]; then
_DEBUG=no _DEBUG=no
SLEEP_TIME=.001 # Tested under linux and FreeBSD bash, #TODO tests on cygwin / msys SLEEP_TIME=.05 # Tested under linux and FreeBSD bash, #TODO tests on cygwin / msys
_VERBOSE=0 _VERBOSE=0
else else
SLEEP_TIME=1 SLEEP_TIME=1
@ -167,9 +167,14 @@ function QuickLogger {
# Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X # Portable child (and grandchild) kill function tester under Linux, BSD and MacOS X
function KillChilds { function KillChilds {
local pid="${1}" local pids="${1}"
local self="${2:-false}" local self="${2:-false}"
local errorcount=0
IFS=';' read -a pidsArray <<< "$pids"
for pid in "${pidsArray[@]}"; do
if children="$(pgrep -P "$pid")"; then if children="$(pgrep -P "$pid")"; then
for child in $children; do for child in $children; do
Logger "Launching KillChilds \"$child\" true" "DEBUG" #__WITH_PARANOIA_DEBUG Logger "Launching KillChilds \"$child\" true" "DEBUG" #__WITH_PARANOIA_DEBUG
@ -187,13 +192,16 @@ function KillChilds {
kill -9 "$pid" kill -9 "$pid"
if [ $? != 0 ]; then if [ $? != 0 ]; then
Logger "Sending SIGKILL to process [$pid] failed." "DEBUG" Logger "Sending SIGKILL to process [$pid] failed." "DEBUG"
return 1 errorcount=$((errorcount+1))
fi fi
fi fi
return 0 #return 0
else else
return 0 echo ""
#return 0
fi fi
done
return $errorcount
} }
# osync/obackup/pmocr script specific mail alert function, use SendEmail function for generic mail sending # osync/obackup/pmocr script specific mail alert function, use SendEmail function for generic mail sending
@ -739,7 +747,10 @@ function WaitForTaskCompletion {
local retval=0 # return value of monitored pid process local retval=0 # return value of monitored pid process
local errorcount=0 # Number of pids that finished with errors local errorcount=0 # Number of pids that finished with errors
local pidCount # number of given pids
IFS=';' read -a pidsArray <<< "$pids" IFS=';' read -a pidsArray <<< "$pids"
pidCount=${#pidsArray[@]}
while [ ${#pidsArray[@]} -gt 0 ]; do while [ ${#pidsArray[@]} -gt 0 ]; do
newPidsArray=() newPidsArray=()
@ -774,13 +785,14 @@ function WaitForTaskCompletion {
fi fi
if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then
Logger "Max hard execution time exceeded for task [$caller_name] with pids [${pidsArray[@]}]. Stopping task execution." "ERROR" Logger "Max hard execution time exceeded for task [$caller_name] with pids [${pidsArray[@]}]. Stopping task execution." "ERROR"
#KillChilds $pid KillChilds $pid
#if [ $? == 0 ]; then if [ $? == 0 ]; then
# Logger "Task stopped successfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
#return 0 #return 0
#else else
errrorcount=$((errorcount+1))
#return 1 #return 1
#fi fi
fi fi
fi fi
@ -788,27 +800,14 @@ function WaitForTaskCompletion {
sleep $SLEEP_TIME sleep $SLEEP_TIME
done done
Logger "${FUNCNAME[0]} ended for [$caller_name] with [$errorcount] errors." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG Logger "${FUNCNAME[0]} ended for [$caller_name] using [$pidCount] subprocesses with [$errorcount] errors." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
if [ $exit_on_error == true ]; then if [ $exit_on_error == true ] && [ $errorcount -gt 0 ]; then
exit 1337 exit 1337
else else
return $errorcount return $errorcount
fi fi
} }
#sleep 2 &
#pids=$!
#sleep 4 &
#pids="$pids;$!"
#sleep 3 &
#pids="$pids;$!"
#WaitForAllTaskCompletion $pids
#sleep 5 &
#WaitForAllTaskCompletion $! 0 0 "me" 1
#exit
function WaitForOldTaskCompletion { function WaitForOldTaskCompletion {
local pid="${1}" # pid to wait for local pid="${1}" # pid to wait for
local soft_max_time="${2}" # If program with pid $pid takes longer than $soft_max_time seconds, will log a warning, unless $soft_max_time equals 0. local soft_max_time="${2}" # If program with pid $pid takes longer than $soft_max_time seconds, will log a warning, unless $soft_max_time equals 0.
@ -862,7 +861,7 @@ function WaitForOldTaskCompletion {
return $retval return $retval
} }
function WaitForTaskCompletion { function WaitForXTaskCompletion {
local pids="${1}" # pids to wait for, separated by semi-colon local pids="${1}" # pids to wait for, separated by semi-colon
local soft_max_time="${2}" # If program with pid $pid takes longer than $soft_max_time seconds, will log a warning, unless $soft_max_time equals 0. local soft_max_time="${2}" # If program with pid $pid takes longer than $soft_max_time seconds, will log a warning, unless $soft_max_time equals 0.
local hard_max_time="${3}" # If program with pid $pid takes longer than $hard_max_time seconds, will stop execution, unless $hard_max_time equals 0. local hard_max_time="${3}" # If program with pid $pid takes longer than $hard_max_time seconds, will stop execution, unless $hard_max_time equals 0.