Operating System - HP-UX
1838703 Members
3397 Online
110128 Solutions
New Discussion

Re: Quick Script Question: Waiting for a (period)

 
SOLVED
Go to solution
Charles Harris
Super Advisor

Quick Script Question: Waiting for a (period)

Dear all,

I need a method of setting a pause from within a script, other than sleep. I want to be able to wait for a random amount of time betweet 0 and 1 sec.

Does anyone have a reliable non hardware dependant solution ?

All tips, pointers, RTFF's , RTFM's warmly received as ever !!

Cheers,

-ChaZ-
13 REPLIES 13
harry d brown jr
Honored Contributor

Re: Quick Script Question: Waiting for a (period)

You could use perl to randomly come up with a value between 0 and 1, then multiply that by some factor, say 100 (or maybe the mhz of the processor), then perform some large calculation in a loop.

live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: Quick Script Question: Waiting for a (period)

Hi Charles,

With perl:

perl -e "select(undef,undef,undef,\$ARGV[0]);" n

will work for fractional values of n.

Rgds, Robin.
Alexander M. Ermes
Honored Contributor

Re: Quick Script Question: Waiting for a (period)

Hi there.
Look at script and check, whether you can use it :


today_=`date +'%y-%m-%d'`; export today_
#
LOG1=/usr/tmp/cpio_${today_}.1st; export LOG1
#
PATH=$PATH:/usr/local/bin
export PATH
exec > /usr/tmp/cpio_${today_}.log 2>&1
#
#
find / -depth -print | cpio -ocBv > /dev/rmt/0m 2>> ${LOG1} &
first_cpio=$!; export first_cpio
#
wait $first_cpio

if test $? -ne 0; then
echo "Full Offline Backup did not complete successfully!"
fi
echo "Time is: `/bin/date`"
echo "Time is: `/bin/date`"
echo "Time is: `/bin/date`" >> ${LOG1}


--------------------
Rgds
Alexander M. Ermes
.. and all these memories are going to vanish like tears in the rain! final words from Rutger Hauer in "Blade Runner"
Robin Wakefield
Honored Contributor

Re: Quick Script Question: Waiting for a (period)

Hi Charles,

Might help if I read the question - this will give you a random sleep between 0 and 1 secs:

perl -e "srand;select(undef,undef,undef,rand);"

Rgds, Robin.
Charles Harris
Super Advisor

Re: Quick Script Question: Waiting for a (period)

Thanks for the tips everyone, I know Perl would be an obvious choice, but I'm stuck with the good old shell.

I really need something available from the shell even if it's not very accurate. I've tried loops etc, but I don't really want to write code that hammers the server into making a delay.

Any other thoughts ?


Thanks again!

-ChaZ-
Krishna Prasad
Trusted Contributor

Re: Quick Script Question: Waiting for a (period)

Why do you need to wait for 0 - 1 second? If 1 second is OK why not sleep 1?

I guess you could just ran a command that doesn't do anything except take up time.
something like

echo "do nothing" > /dev/null
Positive Results requires Positive Thinking
Charles Harris
Super Advisor

Re: Quick Script Question: Waiting for a (period)

Belive it or not I want the delay to a realism to the following code. PLEASE NOTE it is not being used for deception, only to (nicely) illustrate the practical (or not very) use of arrays.

snip:

#!/usr/bin/sh
# scroll
# Don't ask.

typeset -i x=0
strg_len=$(echo "$*" | wc -m)
clear

while (( x < $strg_len )) && (( $strg_len < 1024 ))
do
(( x = x + 1 ))
scroll[x]=`echo "$*" |cut -c $x`
print "${scroll[x]}\c"
sleep 1
done
tput cud1

Cheers,


-ChaZ-
A. Clay Stephenson
Acclaimed Contributor

Re: Quick Script Question: Waiting for a (period)

Hi:

You have a fundamental problem in that sleep has second resolution. However, there is a system call, nanosleep, which has much finer resolution. You could write a very small C program which nanosleeps for an amount of time based upon the random parameter passed in by the calling process - your shell script. The entire C program would be at most 20 lines of code.

If it ain't broke, I can fix that.
Charles Harris
Super Advisor

Re: Quick Script Question: Waiting for a (period)

Thanks again for the tips, by the look's of it I'm going to have to try my hand at C
Could someone point me in the right direction?

Cheers,


-ChaZ-
Andreas Voss
Honored Contributor
Solution

Re: Quick Script Question: Waiting for a (period)

Hi,

write this little c program:

main(argc, argv)
int argc;
char **argv;
{
usleep(atoi(argv[1]));
}

Compile ie:
cc usleep.c -o usleep

Run it ie:
usleep 100

will wait 10 micro seconds before continue.

Regards
A. Clay Stephenson
Acclaimed Contributor

Re: Quick Script Question: Waiting for a (period)

Hi Charles:

Here is my 2 minute C program. Use it like this: nanosleep 4500 to cause a 4500 msec delay or nanosleep 750 to cause a 750msec delay.


Regards, Clay
If it ain't broke, I can fix that.
Robin Wakefield
Honored Contributor

Re: Quick Script Question: Waiting for a (period)

Hi Charles,

Here's one that gives you random tenths of a second delays:

#include
#include

int main()
{
int m;
srand((unsigned long)time(NULL));
m = 100000*(int) (10.0*rand()/(RAND_MAX+1.0));
/* printf("%d\n",m); */
usleep(m);

return 0;
}


Rgds, Robin.
Charles Harris
Super Advisor

Re: Quick Script Question: Waiting for a (period)

Thanks to everyone who helped out with this, the C code has been particularly useful !

I'll get compiling later today and make sure everything's okay.


Cheers!

-ChaZ-