Solved

bash random 24hr event

Posted on 2014-12-01
20
126 Views
Last Modified: 2014-12-07
Using bash, I want to have a function which runs every X amount of time but within a range.
For example, a variable which might be set to 12hrs or 24hrs.

However, I want the function to be randomly set off, between one hour before or after the 24hr period. In other words, I need an event which runs approximately every X amount of time but not exactly on that time.

Looking for bash code as a solution.

Thanks.
0
Comment
Question by:projects
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 5
20 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40475253
while sleep $((82800+RANDOM%7200)) ; do function ; done
0
 

Author Comment

by:projects
ID: 40475740
Could this be added inside of a script which has multiple functions?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40475744
while sleep $((82800+RANDOM%7200)) ; do function1 ; function2 ; function3 ; done
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 250 total points
ID: 40475784
while :
do
  # variable
  PERIODH=12
  PERIODS=$((${PERIODH}*3600))

  # offset (2 meaning +/- 1 hour)
  OFFSETH=2
  OFFSETS=$(echo "scale=0 ; (-16384 + ${RANDOM}) * 3600 / 32768 * ${OFFSETH} " | bc -l)

  # wait before launch
  sleep $((${PERIODS}+${OFFSETS}))

  echo "Put your function call here"
done

Open in new window

0
 

Author Comment

by:projects
ID: 40481876
Darn, turns out this won't work on the device I am using for testing.
I don't have any bc package available and there is an error.

# ./rand
./rand: line 27: bc: command not found
./rand: line 30: 7200+: syntax error: operand expected (error token is "+")
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40481930
What 'device' would that be? If it doesn't even support this line: sleep $((${PERIODS}+${OFFSETS})) then it's going to be hard to calculate anything. You did not test before you accepted a solution? A $((expression)), - and + are supported in bash (see man page).
0
 

Author Comment

by:projects
ID: 40483608
In this case, it is on an openwrt router.
I usually test the solution before hand but in this case, I failed to do that.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40483641
Does the router run bash?  If not, does it use some other scripting language?
0
 

Author Comment

by:projects
ID: 40483908
yes, bash, but no bc and what ever caused that second error.
0
 

Author Comment

by:projects
ID: 40483916
@Gerwin, show me how to test this in a simple bash script and I can post the results.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40483961
while sleep $((82800+RANDOM%7200)) ; do function1 ; function2 ; function3 ; done
does not use bc
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40483964
>> what ever caused that second error
The second error is caused by this line:

$((${PERIODS}+${OFFSETS}))

this is just basic arithmetic in bash, try if this works on your router"

PERIODS=2
OFFSETS=10
echo $((${PERIODS}+${OFFSETS}))

you should get 12 as a result. If that doesn't work then you don't have a normal bash shell. What do you get from:

bash --version
0
 
LVL 84

Expert Comment

by:ozo
ID: 40483975
If the bc failed, and $OFFSETS was blank, then
sleep $((${PERIODS}+${OFFSETS}))
would be
sleep $((7200+))
which would cause an error in a normal bash shell
But a normal bash shell should not need to do
$(echo "scale=0 ; (-16384 + ${RANDOM}) * 3600 / 32768 * ${OFFSETH} " | bc -l)
if it could just do
$(( (-16384 + ${RANDOM}) * 3600 / 32768 * ${OFFSETH} ))
0
 

Author Comment

by:projects
ID: 40483995
@Gerwin; I get a 12.

I know it's pretty standard bash, just doesn't have as many tools as normal since this is just a small embedded device.

# bash --version
GNU bash, version 4.2.53(1)-release (mips-openwrt-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40484012
So the problem is bc not bash, but you don't need bc when you have bash arithmetic.
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40484018
@ozo - Correct, code with your modified line:
while :
do
  # variable
  PERIODH=12
  PERIODS=$((${PERIODH}*3600))

  # offset (2 meaning +/- 1 hour)
  OFFSETH=2
  OFFSETS=$(( (-16384 + ${RANDOM}) * 3600 / 32768 * ${OFFSETH} ))

  # wait before launch
  sleep $((${PERIODS}+${OFFSETS}))

  echo "Put your function call here"
done

Open in new window


works in bash :)
0
 

Author Comment

by:projects
ID: 40484034
Can you change the timer to seconds so I can test this?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40484053
All the timers suggested here sleep in seconds.
0
 

Author Comment

by:projects
ID: 40484129
LOL, yes, just noticed that :)
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40485402
PERIODH = PERIOD in Hours
PERIODS = PERIOD in Seconds

etc. :D
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question