mirror of
https://github.com/deajan/obackup.git
synced 2024-12-26 15:33:41 +01:00
Updated ofunctions
This commit is contained in:
parent
8cef797979
commit
a6f52125b0
@ -1,6 +1,6 @@
|
|||||||
#### MINIMAL-FUNCTION-SET BEGIN ####
|
#### 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
|
## 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:
|
||||||
@ -34,7 +34,7 @@ ERROR_ALERT=false
|
|||||||
WARN_ALERT=false
|
WARN_ALERT=false
|
||||||
|
|
||||||
# Log from current run
|
# Log from current run
|
||||||
CURRENT_LOG=
|
CURRENT_LOG=""
|
||||||
|
|
||||||
## allow function call checks #__WITH_PARANOIA_DEBUG
|
## allow function call checks #__WITH_PARANOIA_DEBUG
|
||||||
if [ "$_PARANOIA_DEBUG" == "yes" ];then #__WITH_PARANOIA_DEBUG
|
if [ "$_PARANOIA_DEBUG" == "yes" ];then #__WITH_PARANOIA_DEBUG
|
||||||
@ -142,8 +142,10 @@ function Logger {
|
|||||||
elif [ "$level" == "NOTICE" ]; then
|
elif [ "$level" == "NOTICE" ]; then
|
||||||
_Logger "$prefix$value"
|
_Logger "$prefix$value"
|
||||||
return
|
return
|
||||||
elif [ "$level" == "VERBOSE" ] && [ $_VERBOSE == true ]; then
|
elif [ "$level" == "VERBOSE" ]; then
|
||||||
_Logger "$prefix$value"
|
if [ $_VERBOSE == true ]; then
|
||||||
|
_Logger "$prefix$value"
|
||||||
|
fi
|
||||||
return
|
return
|
||||||
elif [ "$level" == "DEBUG" ]; then
|
elif [ "$level" == "DEBUG" ]; then
|
||||||
if [ "$_DEBUG" == "yes" ]; then
|
if [ "$_DEBUG" == "yes" ]; then
|
||||||
@ -156,7 +158,7 @@ function Logger {
|
|||||||
return #__WITH_PARANOIA_DEBUG
|
return #__WITH_PARANOIA_DEBUG
|
||||||
fi #__WITH_PARANOIA_DEBUG
|
fi #__WITH_PARANOIA_DEBUG
|
||||||
else
|
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"
|
_Logger "$prefix$value"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@ -611,7 +613,7 @@ function WaitForTaskCompletion {
|
|||||||
Logger "${FUNCNAME[0]} called by [$caller_name]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
Logger "${FUNCNAME[0]} called by [$caller_name]." "PARANOIA_DEBUG" #__WITH_PARANOIA_DEBUG
|
||||||
__CheckArguments 6 $# ${FUNCNAME[0]} "$@" #__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 log_ttime=0 # local time instance for comparaison
|
||||||
|
|
||||||
local seconds_begin=$SECONDS # Seconds since the beginning of the script
|
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 pidsArray # Array of currently running pids
|
||||||
local newPidsArray # New 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"
|
IFS=';' read -a pidsArray <<< "$pids"
|
||||||
pidCount=${#pidsArray[@]}
|
pidCount=${#pidsArray[@]}
|
||||||
|
|
||||||
@ -652,9 +656,9 @@ function WaitForTaskCompletion {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $exec_time -gt $soft_max_time ]; then
|
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"
|
Logger "Max soft execution time exceeded for task [$caller_name] with pids [$(joinString , ${pidsArray[@]})]." "WARN"
|
||||||
soft_alert=1
|
soft_alert=true
|
||||||
SendAlert true
|
SendAlert true
|
||||||
|
|
||||||
fi
|
fi
|
||||||
@ -674,29 +678,36 @@ function WaitForTaskCompletion {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
for pid in "${pidsArray[@]}"; do
|
for pid in "${pidsArray[@]}"; do
|
||||||
if kill -0 $pid > /dev/null 2>&1; then
|
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
|
||||||
#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
|
||||||
|
hasPids=true ##__WITH_PARANOIA_DEBUG
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ $hasPids == false ]; then ##__WITH_PARANOIA_DEBUG
|
||||||
|
Logger "No valable pids given." "ERROR" ##__WITH_PARANOIA_DEBUG
|
||||||
|
fi ##__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
pidsArray=("${newPidsArray[@]}")
|
pidsArray=("${newPidsArray[@]}")
|
||||||
# Trivial wait time for bash to not eat up all CPU
|
# Trivial wait time for bash to not eat up all CPU
|
||||||
sleep $SLEEP_TIME
|
sleep $SLEEP_TIME
|
||||||
@ -729,6 +740,8 @@ function ParallelExec {
|
|||||||
local pidState
|
local pidState
|
||||||
local commandsArrayPid
|
local commandsArrayPid
|
||||||
|
|
||||||
|
local hasPids=false # Are any valable pids given to function ? #__WITH_PARANOIA_DEBUG
|
||||||
|
|
||||||
IFS=';' read -r -a commandsArray <<< "$commandsArg"
|
IFS=';' read -r -a commandsArray <<< "$commandsArg"
|
||||||
|
|
||||||
Logger "Runnning ${#commandsArray[@]} commands in $numberOfProcesses simultaneous processes." "DEBUG"
|
Logger "Runnning ${#commandsArray[@]} commands in $numberOfProcesses simultaneous processes." "DEBUG"
|
||||||
@ -747,22 +760,29 @@ 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 [ $(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 ? :)
|
||||||
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
|
||||||
|
hasPids=true ##__WITH_PARANOIA_DEBUG
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ $hasPids == false ]; then ##__WITH_PARANOIA_DEBUG
|
||||||
|
Logger "No valable pids given." "ERROR" ##__WITH_PARANOIA_DEBUG
|
||||||
|
fi ##__WITH_PARANOIA_DEBUG
|
||||||
pidsArray=("${newPidsArray[@]}")
|
pidsArray=("${newPidsArray[@]}")
|
||||||
|
|
||||||
# Trivial wait time for bash to not eat up all CPU
|
# Trivial wait time for bash to not eat up all CPU
|
||||||
|
Loading…
Reference in New Issue
Block a user