Made WaitFor and ParallelExec functions more robust

This commit is contained in:
deajan 2016-08-30 11:37:25 +02:00
parent 9804334821
commit a31cceedc6
1 changed files with 36 additions and 32 deletions

View File

@ -1,6 +1,6 @@
#### MINIMAL-FUNCTION-SET BEGIN #### #### MINIMAL-FUNCTION-SET BEGIN ####
## FUNC_BUILD=2016082902 ## FUNC_BUILD=2016083001
## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## To use in a program, define the following variables: ## To use in a program, define the following variables:
@ -676,24 +676,26 @@ function WaitForTaskCompletion {
fi fi
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
if kill -0 $pid > /dev/null 2>&1; then if [ "$pid" != "" ]; then
# Handle uninterruptible sleep state or zombies by ommiting them from running process array (How to kill that is already dead ? :) if kill -0 $pid > /dev/null 2>&1; then
#TODO(high): have this tested on *BSD, Mac & Win # Handle uninterruptible sleep state or zombies by ommiting them from running process array (How to kill that is already dead ? :)
pidState=$(ps -p$pid -o state= 2 > /dev/null) #TODO(high): have this tested on *BSD, Mac & Win
if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then pidState=$(ps -p$pid -o state= 2 > /dev/null)
newPidsArray+=($pid) if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then
fi newPidsArray+=($pid)
else fi
# pid is dead, get it's exit code from wait command else
wait $pid # pid is dead, get it's exit code from wait command
retval=$? wait $pid
if [ $retval -ne 0 ]; then retval=$?
errorcount=$((errorcount+1)) if [ $retval -ne 0 ]; then
Logger "${FUNCNAME[0]} called by [$caller_name] finished monitoring [$pid] with exitcode [$retval]." "DEBUG" errorcount=$((errorcount+1))
if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then Logger "${FUNCNAME[0]} called by [$caller_name] finished monitoring [$pid] with exitcode [$retval]." "DEBUG"
WAIT_FOR_TASK_COMPLETION="$pid:$retval" if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then
else WAIT_FOR_TASK_COMPLETION="$pid:$retval"
WAIT_FOR_TASK_COMPLETION=";$pid:$retval" else
WAIT_FOR_TASK_COMPLETION=";$pid:$retval"
fi
fi fi
fi fi
fi fi
@ -749,19 +751,21 @@ function ParallelExec {
newPidsArray=() newPidsArray=()
for pid in "${pidsArray[@]}"; do for pid in "${pidsArray[@]}"; do
# Handle uninterruptible sleep state or zombies by ommiting them from running process array (How to kill that is already dead ? :) if [ "$pid" != "" ]; then
if kill -0 $pid > /dev/null 2>&1; then # Handle uninterruptible sleep state or zombies by ommiting them from running process array (How to kill that is already dead ? :)
pidState=$(ps -p$pid -o state= 2 > /dev/null) if kill -0 $pid > /dev/null 2>&1; then
if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then pidState=$(ps -p$pid -o state= 2 > /dev/null)
newPidsArray+=($pid) if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then
fi newPidsArray+=($pid)
else fi
# pid is dead, get it's exit code from wait command else
wait $pid # pid is dead, get it's exit code from wait command
retval=$? wait $pid
if [ $retval -ne 0 ]; then retval=$?
Logger "Command [${commandsArrayPid[$pid]}] failed with exit code [$retval]." "ERROR" if [ $retval -ne 0 ]; then
retvalAll=$((retvalAll+1)) Logger "Command [${commandsArrayPid[$pid]}] failed with exit code [$retval]." "ERROR"
retvalAll=$((retvalAll+1))
fi
fi fi
fi fi
done done