From 3ce4a2f4969c70aac4df6bc52a81bb511ee27f4d Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" Date: Wed, 9 Mar 2022 16:32:38 +0100 Subject: [PATCH] add cw.randomsleep --- docs/20_Usage/40_Helper_functions.md | 10 +++++++++- inc_cronfunctions.sh | 24 +++++++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/docs/20_Usage/40_Helper_functions.md b/docs/20_Usage/40_Helper_functions.md index 28f1db2..dfe9b61 100644 --- a/docs/20_Usage/40_Helper_functions.md +++ b/docs/20_Usage/40_Helper_functions.md @@ -20,7 +20,7 @@ After sourcing inc_cronfunctions.sh you get a list of available function with `c ```txt HELP FOR CRONWRAPPER FUNCTIONS -auto generated list of implemented cw.\* functions +auto generated list of implemented cw.* functions cw.cecho colored echo output using color and reset color afterwards @@ -59,6 +59,14 @@ cw.quit quit script with showing the total exitcode. no parameter is required +cw.randomsleep + sleep for a random time + param integer time to randomize in sec + param integer optional: minimal time to sleep in sec; default: 0 + Example: + cw.randomsleep 60 sleeps for a random time 0..60 sec + cw.randomsleep 60 30 sleeps for a random time 30..90 sec + cw.timer get time in sec and milliseconds since start no parameter is required diff --git a/inc_cronfunctions.sh b/inc_cronfunctions.sh index 11bf605..7312448 100644 --- a/inc_cronfunctions.sh +++ b/inc_cronfunctions.sh @@ -102,7 +102,7 @@ function cw.help(){ cw.cecho cmd $fktname typeset -i local _linestart _linestart=$( grep -En "function\ $fktname\ *\(" "$_self" | cut -f 1 -d ':' ) - typeset -i local _print_start=$_linestart-5 + typeset -i local _print_start=$_linestart-6 sed -n ${_print_start},${_linestart}p $_self | grep '^#' | grep -v "\-\-\-\-\-" | cut -c 3- | sed "s#^# #g" echo @@ -136,6 +136,20 @@ function cw.exec() { } +# sleep for a random time +# param integer time to randomize in sec +# param integer optional: minimal time to sleep in sec; default: 0 +# Example: +# cw.randomsleep 60 sleeps for a random time 0..60 sec +# cw.randomsleep 60 30 sleeps for a random time 30..90 sec +function cw.randomsleep(){ + typeset -i local _iRnd=$1 + typeset -i local _iMintime=$2 + typeset -i local _iSleep=$(($RANDOM%$_iRnd+$_iMintime)) + echo Sleeping for $_iSleep sec ... + sleep $_iSleep +} + # get time in sec and milliseconds since start # no parameter is required function cw.timer(){ @@ -164,9 +178,7 @@ function cw.quit(){ exit $rcAll } -# ---------------------------------------------------------------------- -# coloring -# ---------------------------------------------------------------------- +# ----- coloring ------------------------------------------------------- # set a terminal color by a keyword # param string keyword to set a color; one of reset | head|cmd|input | ok|warning|error @@ -201,9 +213,7 @@ function cw.cecho (){ cw.color "$_color"; echo -n "$*"; cw.color reset; echo } -# ---------------------------------------------------------------------- -# locking -# ---------------------------------------------------------------------- +# ----- locking -------------------------------------------------------- # helper function: generate a filename for locking function cw._getlockfilename(){