Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

sleep is a very popular command and we can start sleep from 1 second:

# wait one second please 
sleep 1

but what the alternative if I need to wait only 0.1 second or between 0.1 to 1 second ?

  • remark: on linux sleep 0.XXX works fine , but on solaris sleep 0.1 or sleep 0.01 - illegal syntax
share|improve this question
2  
Can I ask why you want to sleep for 1ms? –  Tom O'Connor Jan 15 '13 at 14:03
    
Yes of course , in my bash script I add "sleep 1" , in some lines , but script run very slowly , so after some conclusion I calculate that sleep 0.1 also bring good results and more faster About the delay , I need delay in order to solve the ssh problem in my bash script , I perform paralel ssh login to some machines by expect and without delay its will not work , As you know from my question the delay should fit both Linux and Solaris –  yael Jan 15 '13 at 14:09
1  
Whatever solution you choose, keep in mind that a shell script won't be very accurate in terms of timing. –  scai Jan 15 '13 at 14:34
    
How about doing something that takes a very short time to execute, but does nothing.. like echo "" >/dev/null –  Tom O'Connor Jan 15 '13 at 15:01
    
Good idea but how msec this command take? , I need 0.1 msec , not less then that -:) –  yael Jan 15 '13 at 15:12

4 Answers 4

up vote 16 down vote accepted

Bash has a "loadable" sleep which supports fractional seconds:

$ cd bash-3.2.48/examples/loadables
$ make sleep && mv sleep sleep.so
$ enable -f sleep.so sleep

Then:

$ which sleep
/usr/bin/sleep
$ builtin sleep
sleep: usage: sleep seconds[.fraction]
$ time (for f in `seq 1 10`; do sleep 0.1; done)
real    0m1.000s
user    0m0.004s
sys     0m0.004s

The downside is that it may not be provided with your bash binary, so you would need to compile it yourself, as shown (though on Solaris it would not necessarily be as simple as I have shown above).

If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature.

As a last resort you could use perl (or any other scripting that you have to hand) with the caveat that initialising the interpreter may be comparable to the intended sleep time:

$ perl -e "select(undef,undef,undef,0.1);"
$ echo "after 100" | tclsh
share|improve this answer
1  
Ah, since you're using expect you can probably just use "after N", where N is milliseconds, directly in your script. –  mr.spuratic Jan 15 '13 at 14:46

The documentation for the sleep command from coreutils says:

Historical implementations of sleep have required that number be an integer, and only accepted a single argument without a suffix. However, GNU sleep accepts arbitrary floating point numbers. See Floating point.

Hence you can use sleep 0.1, sleep 1.0e-1 and similar arguments.

share|improve this answer
    
see my remark about SOLARIS OS –  yael Jan 15 '13 at 13:37
    
Did you mix up is and isn't? –  scai Jan 15 '13 at 13:42
    
see my update in my quastion –  yael Jan 15 '13 at 13:45
1  
Yael, I think there're still one too many negatives in your question; are you sure you mean "not illegal syntax"? –  MadHatter Jan 15 '13 at 14:23
    
for example - I run on solaris 10 this: # sleep 0.1 sleep: bad character in argument , about linux sleep 0.1 works fine –  yael Jan 15 '13 at 14:37

Sleep accepts decimal numbers so you can break it down this like:

1/2 of a second

 sleep 0.5

1/100 of a second

sleep 0.01

So for a millisecond you would want

sleep 0.001
share|improve this answer
1  
You can also drop the leading zero before the decimal point. eg. sleep .5 –  Mike Causer Jun 22 at 7:13

Try this to determine accuracy:

    time sleep 0.5      # 500 milliseconds (1/2 of a second)
    time sleep 0.001    # 1 millisecond (1/1000 of a second)
    time sleep 1.0      # 1 second (1000 milliseconds)

Combination of mr.spuratic's solution and coles's solution.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.