From a6f52125b03b54734a6626dc55a40dd243f3d6ce Mon Sep 17 00:00:00 2001 From: deajan Date: Tue, 30 Aug 2016 18:55:36 +0200 Subject: [PATCH] Updated ofunctions --- dev/ofunctions.sh | 98 ++++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index acf3fc2..51f598d 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -1,6 +1,6 @@ #### MINIMAL-FUNCTION-SET BEGIN #### -## FUNC_BUILD=2016082901 +## FUNC_BUILD=2016083003 ## 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: @@ -34,7 +34,7 @@ ERROR_ALERT=false WARN_ALERT=false # Log from current run -CURRENT_LOG= +CURRENT_LOG="" ## allow function call checks #__WITH_PARANOIA_DEBUG if [ "$_PARANOIA_DEBUG" == "yes" ];then #__WITH_PARANOIA_DEBUG @@ -142,8 +142,10 @@ function Logger { elif [ "$level" == "NOTICE" ]; then _Logger "$prefix$value" return - elif [ "$level" == "VERBOSE" ] && [ $_VERBOSE == true ]; then - _Logger "$prefix$value" + elif [ "$level" == "VERBOSE" ]; then + if [ $_VERBOSE == true ]; then + _Logger "$prefix$value" + fi return elif [ "$level" == "DEBUG" ]; then if [ "$_DEBUG" == "yes" ]; then @@ -156,7 +158,7 @@ function Logger { return #__WITH_PARANOIA_DEBUG fi #__WITH_PARANOIA_DEBUG else - _Logger "\e[41mLogger function called without proper loglevel.\e[0m" + _Logger "\e[41mLogger function called without proper loglevel [$level].\e[0m" _Logger "$prefix$value" fi } @@ -611,7 +613,7 @@ function WaitForTaskCompletion { Logger "${FUNCNAME[0]} called by [$caller_name]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG __CheckArguments 6 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG - local soft_alert=0 # Does a soft alert need to be triggered, if yes, send an alert once + local soft_alert=false # Does a soft alert need to be triggered, if yes, send an alert once local log_ttime=0 # local time instance for comparaison local seconds_begin=$SECONDS # Seconds since the beginning of the script @@ -627,6 +629,8 @@ function WaitForTaskCompletion { local pidsArray # Array of currently running pids local newPidsArray # New array of currently running pids + local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG + IFS=';' read -a pidsArray <<< "$pids" pidCount=${#pidsArray[@]} @@ -652,9 +656,9 @@ function WaitForTaskCompletion { fi if [ $exec_time -gt $soft_max_time ]; then - if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then + if [ $soft_alert == true ] && [ $soft_max_time -ne 0 ]; then Logger "Max soft execution time exceeded for task [$caller_name] with pids [$(joinString , ${pidsArray[@]})]." "WARN" - soft_alert=1 + soft_alert=true SendAlert true fi @@ -674,29 +678,36 @@ function WaitForTaskCompletion { fi for pid in "${pidsArray[@]}"; do - 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 ? :) - #TODO(high): have this tested on *BSD, Mac & Win - pidState=$(ps -p$pid -o state= 2 > /dev/null) - if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then - newPidsArray+=($pid) - fi - else - # pid is dead, get it's exit code from wait command - wait $pid - retval=$? - if [ $retval -ne 0 ]; then - errorcount=$((errorcount+1)) - Logger "${FUNCNAME[0]} called by [$caller_name] finished monitoring [$pid] with exitcode [$retval]." "DEBUG" - if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then - WAIT_FOR_TASK_COMPLETION="$pid:$retval" - else - WAIT_FOR_TASK_COMPLETION=";$pid:$retval" + if [ $(IsNumeric $pid) -eq 1 ]; 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 ? :) + #TODO(high): have this tested on *BSD, Mac & Win + pidState=$(ps -p$pid -o state= 2 > /dev/null) + if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then + newPidsArray+=($pid) + fi + else + # pid is dead, get it's exit code from wait command + wait $pid + retval=$? + if [ $retval -ne 0 ]; then + errorcount=$((errorcount+1)) + Logger "${FUNCNAME[0]} called by [$caller_name] finished monitoring [$pid] with exitcode [$retval]." "DEBUG" + if [ "$WAIT_FOR_TASK_COMPLETION" == "" ]; then + WAIT_FOR_TASK_COMPLETION="$pid:$retval" + else + WAIT_FOR_TASK_COMPLETION=";$pid:$retval" + fi fi fi + hasPids=true ##__WITH_PARANOIA_DEBUG fi done + if [ $hasPids == false ]; then ##__WITH_PARANOIA_DEBUG + Logger "No valable pids given." "ERROR" ##__WITH_PARANOIA_DEBUG + fi ##__WITH_PARANOIA_DEBUG + pidsArray=("${newPidsArray[@]}") # Trivial wait time for bash to not eat up all CPU sleep $SLEEP_TIME @@ -729,6 +740,8 @@ function ParallelExec { local pidState local commandsArrayPid + local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG + IFS=';' read -r -a commandsArray <<< "$commandsArg" Logger "Runnning ${#commandsArray[@]} commands in $numberOfProcesses simultaneous processes." "DEBUG" @@ -747,22 +760,29 @@ function ParallelExec { newPidsArray=() 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 kill -0 $pid > /dev/null 2>&1; then - pidState=$(ps -p$pid -o state= 2 > /dev/null) - if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then - newPidsArray+=($pid) - fi - else - # pid is dead, get it's exit code from wait command - wait $pid - retval=$? - if [ $retval -ne 0 ]; then - Logger "Command [${commandsArrayPid[$pid]}] failed with exit code [$retval]." "ERROR" - retvalAll=$((retvalAll+1)) + if [ $(IsNumeric $pid) -eq 1 ]; 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 + pidState=$(ps -p$pid -o state= 2 > /dev/null) + if [ "$pidState" != "D" ] && [ "$pidState" != "Z" ]; then + newPidsArray+=($pid) + fi + else + # pid is dead, get it's exit code from wait command + wait $pid + retval=$? + if [ $retval -ne 0 ]; then + Logger "Command [${commandsArrayPid[$pid]}] failed with exit code [$retval]." "ERROR" + retvalAll=$((retvalAll+1)) + fi fi + hasPids=true ##__WITH_PARANOIA_DEBUG fi done + + if [ $hasPids == false ]; then ##__WITH_PARANOIA_DEBUG + Logger "No valable pids given." "ERROR" ##__WITH_PARANOIA_DEBUG + fi ##__WITH_PARANOIA_DEBUG pidsArray=("${newPidsArray[@]}") # Trivial wait time for bash to not eat up all CPU