8

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.

A.H.
  • 63,967
  • 15
  • 92
  • 126
Raihan
  • 10,095
  • 5
  • 27
  • 45
  • 2
    ask 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. – shellter Oct 13 '11 at 17:30

6 Answers6

7

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.

Chriszuma
  • 4,464
  • 22
  • 19
  • I believe this is only true in the GNU version of sleep. – Kurt Stutsman Oct 13 '11 at 17:05
  • How do you get the version of sleep? sleep -v or sleep --version doesn't work for me. – Raihan Oct 13 '11 at 17:18
  • the 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." – Chriszuma Oct 13 '11 at 17:21
  • 1
    Of 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) for `time for i in {1..1000}; do sleep 0.002; done` – James Waldby - jwpat7 Oct 15 '11 at 18:23
4

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • This 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). – Stéphane Jun 06 '13 at 15:09
1

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
Orwellophile
  • 13,235
  • 3
  • 69
  • 45
0

A very very very simply pythonesque usleep in decimal second fractions. It is NOT very precise, and no error checking on command line args

#!/usr/bin/python
import sys
import time

if len(sys.argv) == 1:
    sleepTime = 1.0
else:
    sleepTime = str(sys.argv[1])

time.sleep(float(sleepTime))
0

Have you tried looking at the man pages? It should have a way to do a delay that is less than a second, I am not a Linux machine right now so can't look it up for you.

island_hopper
  • 285
  • 1
  • 2
  • 8
0

You can use usleep. Here's a link to the man page: http://linuxmanpages.com/man1/usleep.1.php

  • I don't seem to have usleep in my bash/solaris. – Raihan Oct 13 '11 at 17:19
  • You may have to write a method to add to a number in a loop then. Like while foo is less than a large number, increment foo. Then you'd just have to change the large number depending on your computer. Although that is kind of wasteful because you're just sitting there adding. –  Oct 13 '11 at 18:56