Bash: How to make short delay?
Solution 1
SunOS (Solaris) probably doesn't have the GNU tools installed by default. You might consider installing them. It's also possible that they're already installed in your system, perhaps in some directory that isn't in your default $PATH
. GNU sleep
is part of the coreutils package.
If you have Perl, then this:
perl -MTime::HiRes -e 'Time::HiRes::usleep 500000'
should sleep for 500000 microseconds (0.5 second) -- but the overhead of invoking perl is substantial.
For minimal overhead, I'd write a small C program that calls usleep()
or nanosleep()
. Note that usleep()
might not handle intervals greater than 1 second.
Solution 2
I don't know what version this was implemented in, but my version of sleep (v6.12) accepts decimals. sleep 0.5
works.
If yours is too old for that, a short python or C program would probably be your only solution.
Solution 3
Write this to "usleep.c"
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
usleep( atol( argv[1] ) );
}
And type
make usleep
./usleep 1000000
Related videos on Youtube
Raihan
Updated on June 04, 2022Comments
-
Raihan almost 2 years
How to make a short delay (for less than a second) in bash? The smallest time unit in sleep command is 1 sec. I am using bash 3.0 in SunOS 5.10.
-
shellter over 12 yearsask your solaris admins if your system has GNU utilities installed and what is the path to them. Then you should find the sleep in that dir will be the GNU version that will accept floating point args. Good luck.
-
-
Kurt Stutsman over 12 yearsI believe this is only true in the GNU version of sleep.
-
Raihan over 12 yearsHow do you get the version of sleep? sleep -v or sleep --version doesn't work for me.
-
Chriszuma over 12 yearsthe Man page has it at the bottom. EDIT: haha, I just noticed it also says "Unlike most implementations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number."
-
James Waldby - jwpat7 over 12 yearsOf course the timing might not be highly accurate. E.g., error was about 5% (2.113 seconds total) for following command:
time for i in {1..100}; do sleep 0.02; done
and was 50% (3.004s total) fortime for i in {1..1000}; do sleep 0.002; done
-
Stéphane almost 11 yearsThis works correctly but invoking perl takes a little time (Calling 20 times your perl command on my system with 100ms took 2.6 seconds) so I've preferred Chriszuma's solution (sleep 0.1, 20 calls took 2.089 seconds).