Sleep for less than 1 second

Q: I need to sleep in my script for less than a second. How can this be done?

 A:  While you might perform some calculation a million times, your script will run slow on older systems and too fast on new systems. perl
is your friend in this case. By using the select statement with null values, a fractional sleep value can be specified. To sleep (actually, pause) for 150ms, use this:

perl -e ‘select(undef,undef,undef,.150)’

This will wait for 0.150 seconds. This is useful for creating a wait-spinner, that is, a series of ASCII characters that look like a wheel or spinner. A sleep 1 command is too slow for a spinner. Here is a function that you can call repeatedly and it will spin ASCII characters with each call.

function BusyFlag
{

# NOTE: Needs perl in the current $PATH list #####################################################
# Call this function to display a rotating ASCII art
# wheel. Uses perl to pause for 0.15 seconds
# between characters. Leave the cursor to the
# right of the spinner so it can be seen.
set -A WHEEL “|” “/” “-” “\\”
CNT=${CNT:-0}
CHR=$((CNT%4))

echo “${WHEEL[$CHR]} \c”
CNT=$((CNT+1))
perl -e ‘select(undef,undef,undef,.15)’
echo “\b\b\c”
return
}

A possible application of this function is to provide feedback about a long running set process or series of processes. You would start the process(es) in the background. Then start a loop that first calls the BusyFlag function, then checks the status of the process(es). An enhancement might be to run the BusyFlag loop 100 to 200 times (20-35 seconds), then perform the check on the remaining background processes and print a progress report such as the number remaining.

– See more at: http://serviceitdirect.com/blog/sleep-less-1-second#sthash.a278ddGt.dpuf


Tags: