top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Shell Scripting Random Delay

+2 votes
50 views

I have a shell script that's run every 5 minutes I use to call many other shell scripts. Is there a way to wait a random number of seconds before executing each line? Something like this.

wait_random 10 - 180 (perl /scripts/my_script.pl) &
wait_random 10 - 180 (perl /scripts/my_script5.pl) &
wait_random 10 - 180 (perl /scripts/my_script7.pl) &

I have many entries in this file and I background them all because most must poll network devices which can take time. None should take over 2 minutes though.

When I run them all at once they bog the system and cause some of latency graphs on equipment being monitored to look poor.

posted May 15, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

4 Answers

+1 vote

Something like

sleep $(($(date +%S)*3))

More random would be to use nanoseconds (see date manpage), but then you'd have to cook up an algorithm to test and toss out values (arguments to 'sleep') which you didn't want.

answer May 15, 2013 by anonymous
+1 vote

Use your script to generate a random number http://islandlinux.org/howto/generate-random-numbers-bash-scripting and pass that to the sleep command?

answer May 15, 2013 by anonymous
+1 vote

There's a better answer: do some testing to find out just how many you can do at a time without the network bogging down, then

for ( $list  ) {
 my_script.pl $list
}

With the datafile being something like

#fast device responders
dev1 dev2 dev3 dev4
dev5 dev6 dev7 dev8
#slow device responders
sdev1 sdev2
sdev3 sdev4

And make your script do all of the first line, then wait for responses.

answer May 15, 2013 by anonymous
+1 vote

For bash terminal we get random value from this variable
$RANDOM

Each time if you echo $RANDOM, it will give a random value, you can take this value mode (%) with your preferred range

For example:- i would like to get the random value between 0-9
echo $((echo $RANDOM % 10))

In your case you can:-
sleep $((echo $RANDOM % 10))

answer Jun 28, 2013 by Azad Nikarthil
Similar Questions
+2 votes

We develop embedded software for 32-bit micros using Windows as the development platform.

We are seeking a general purpose scripting language to automate certain tasks, like cleaning out certain directories of certain types of files in preparation for ZIP'ing, generating certain source files automatically, etc.

Selection criteria:

a)Should be able to compile the script interpreter as a monolithic executable (no .DLL dependencies, etc.) for easy versioning and distribution of the script interpreter. (Note that I'm not asking that the script be a single executable, just the interpreter. To run a script you'd need both the script and the interpreter. The script would be a text file, and the interpreter would be a single .EXE.)

b)Should be extensible, in that one could add commands or library functions to the script interpreter in C (for efficiency), and the whole script interpreter could again consist of a single executable with no other dependencies. (Note that I'm not asking that the script be a single executable, just the interpreter. To run a script you'd need both the script and the interpreter. The script would be a text file, and the interpreter would be a single .EXE.)

c)Should be able to spawn compilers and capture the output, do file I/O, and all the other minor expected stuff.

d)Graphical capability would be nice.

I know that Tcl/Tk would do all of the above, but what about Python or any other alternatives?

0 votes
+2 votes

I am using below script to validate IPV6 address but this script also pass invalid IP such as empty string, name etc.

if [[ "$IPv6ADDR"=~"^:?([a-fA-F0-9]{1,4}(:|.)?){0,8}(:|::)?([a-fA-F0-9]{1,4}(:|.)?){0,8}$" ]]; then
  echo "valid ipv6 addr"
  break;
else
  echo "Invalid IP Address"
  break;
fi

Can someone identify what's wrong in the regex, Please?

+2 votes

I have a process in Linux which can have different memory footprint, I want a program or shell script which can print the time of the day when the memory usage is highest for this process.

Can someone help me.


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...