top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Shell Scripting Random Delay

+2 votes
928 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 LinkedIn 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

how to raise a shell script to raise an alarm o the Linux system when the system disk space exceeds the 90% usage

+1 vote

I have thousands of html files inside a folder. I want to replace the filename present inside another files. Say for ex:- fileName: 'abcd1234.html' is found inside another file say file2.html. Then I want to remove the last 4 digits of the fileName i.e,. 'abcd1234.html' => 'abcd.htm'.

0 votes
...