Raihan
Raihan

Reputation: 10405

Bash: How to make short delay?

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.

Upvotes: 9

Views: 10649

Answers (6)

Marc Volovic
Marc Volovic

Reputation: 1

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))

Upvotes: 0

Orwellophile
Orwellophile

Reputation: 13933

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

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263237

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.

Upvotes: 4

Chriszuma
Chriszuma

Reputation: 4557

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.

Upvotes: 8

user993930
user993930

Reputation:

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

Upvotes: 0

island_hopper
island_hopper

Reputation: 285

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.

Upvotes: 0

Related Questions