Skip to content

Commit

Permalink
add cw.randomsleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Hahn Axel (hahn) committed Mar 9, 2022
1 parent 0c84082 commit 3ce4a2f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
10 changes: 9 additions & 1 deletion docs/20_Usage/40_Helper_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions inc_cronfunctions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(){
Expand Down

0 comments on commit 3ce4a2f

Please sign in to comment.