How do I sleep for a millisecond in bash or kshDoes the bash usleep block? Or will it yield to other...
How to deal with or prevent idle in the test team?
A known event to a history junkie
Simple image editor tool to draw a simple box/rectangle in an existing image
I2C signal and power over long range (10meter cable)
Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?
Perfect riffle shuffles
Can I use my Chinese passport to enter China after I acquired another citizenship?
How to interpret the phrase "t’en a fait voir à toi"?
Adding empty element to declared container without declaring type of element
Stereotypical names
Is there any significance to the Valyrian Stone vault door of Qarth?
Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?
Is infinity mathematically observable?
Is there a problem with hiding "forgot password" until it's needed?
Is there an Impartial Brexit Deal comparison site?
Is the next prime number always the next number divisible by the current prime number, except for any numbers previously divisible by primes?
Is it possible to build a CPA Secure encryption scheme which remains secure even when the encryption of secret key is given?
Simulating a probability of 1 of 2^N with less than N random bits
How to prevent YouTube from showing already watched videos?
What if somebody invests in my application?
Is there a good way to store credentials outside of a password manager?
Giant Toughroad SLR 2 for 200 miles in two days, will it make it?
Freedom of speech and where it applies
Blender - show edges angles “direction”
How do I sleep for a millisecond in bash or ksh
Does the bash usleep block? Or will it yield to other threads?How to determine if a bash variable is empty?How can I sort du -h output by sizeawk + perl + get two arguments in to awk syntaxbash + print line every 10 min in bashAnyone else experiencing high rates of Linux server crashes during a leap second day?expect script + write “if” in expect scriptperl + sed + remove lines that start with “#”shell: create Shortcut command (alias or function) for working with IP address as argumentsHow can I know the absolute path of a running process? on solarisWaiting for LVM initialisation in AWS
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 or OS X
sleep 0.XXXworks fine , but on solarissleep 0.1orsleep 0.01- illegal syntax
linux bash solaris shell-scripting ksh
|
show 3 more comments
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 or OS X
sleep 0.XXXworks fine , but on solarissleep 0.1orsleep 0.01- illegal syntax
linux bash solaris shell-scripting ksh
2
Can I ask why you want to sleep for 1ms?
– Tom O'Connor
Jan 15 '13 at 14:03
1
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
2
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.. likeecho "" >/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
|
show 3 more comments
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 or OS X
sleep 0.XXXworks fine , but on solarissleep 0.1orsleep 0.01- illegal syntax
linux bash solaris shell-scripting ksh
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 or OS X
sleep 0.XXXworks fine , but on solarissleep 0.1orsleep 0.01- illegal syntax
linux bash solaris shell-scripting ksh
linux bash solaris shell-scripting ksh
edited Aug 29 '18 at 13:52
rogerdpack
468416
468416
asked Jan 15 '13 at 13:19
yaelyael
86331635
86331635
2
Can I ask why you want to sleep for 1ms?
– Tom O'Connor
Jan 15 '13 at 14:03
1
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
2
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.. likeecho "" >/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
|
show 3 more comments
2
Can I ask why you want to sleep for 1ms?
– Tom O'Connor
Jan 15 '13 at 14:03
1
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
2
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.. likeecho "" >/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
2
2
Can I ask why you want to sleep for 1ms?
– Tom O'Connor
Jan 15 '13 at 14:03
Can I ask why you want to sleep for 1ms?
– Tom O'Connor
Jan 15 '13 at 14:03
1
1
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
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
2
2
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
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
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
Good idea but how msec this command take? , I need 0.1 msec , not less then that -:)
– yael
Jan 15 '13 at 15:12
|
show 3 more comments
6 Answers
6
active
oldest
votes
Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
$ 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 builtin sleep 0.1; done)
real 0m1.000s
user 0m0.004s
sys 0m0.004s
The downside is that the loadables may not be provided with your bash binary, so you would need to compile them yourself as shown (though on Solaris it would not necessarily be as simple as above).
As of bash-4.4 (September 2016) all the loadables are now built and installed by default on platforms that support it, though they are built as separate shared-object files, and without a .so suffix. Unless your distro/OS has done something creative, you should be able to do instead:
[ -z "$BASH_LOADABLES_PATH" ] &&
BASH_LOADABLES_PATH=$(pkg-config bash --variable=loadablesdir 2>/dev/null)
enable -f sleep sleep
(The man page implies BASH_LOADABLES_PATH is set automatically, I find this is not the case in the official distribution as of 4.4.12. If and when it is set correctly you need only enable -f filename commandname as required.)
If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature. The POSIX sleep command is minimal, older Solaris versions implemented only that. Solaris 11 sleep does support fractional seconds.
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
2
Ah, since you're usingexpectyou can probably just use "after N", where N is milliseconds, directly in your script.
– mr.spuratic
Jan 15 '13 at 14:46
useusleeplike @Luis Vazquez and @sebix write
– Ilan.K
Feb 20 '16 at 9:36
add a comment |
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.
1
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
|
show 1 more comment
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
4
You can also drop the leading zero before the decimal point. eg.sleep .5
– Mike Causer
Jun 22 '14 at 7:13
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
add a comment |
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.
add a comment |
You may simply use usleep. It takes microseconds (= 1e-6 seconds) as parameter, so to sleep 1 millisecond you would enter:
usleep 1000
1
$ usleepNo command 'usleep' found, did you mean:Command 'sleep' from package 'coreutils' (main)usleep: command not found
– Bulletmagnet
Apr 5 '17 at 14:18
No, i meanusleeppart of theinitscriptspackage which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``
– Luis Vazquez
Jul 15 '17 at 17:14
1
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -sleep(from coreutils) works with seconds -usleep(from initscripts) works with micro-seconds
– Luis Vazquez
Jul 15 '17 at 17:22
add a comment |
I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
#include "stdio.h"
int main(int argc, char **argv) {
if(argc == 2) { usleep(atoi(argv[1])); }
return 0;
}
Doesn't check arguments - I'd recommend a properly written one if you wanted to keep it but that (gcc usleep.c -o usleep) will get you out of a hole.
1
You could at least change that bareusleep()call toif(argc == 1) { usleep(atoi(argv[1])); }to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.
– a CVn
Oct 13 '16 at 15:05
@aCVn It's actuallyif (argc == 2) { usleep(atoi(argv[1])); }...
– Ring Ø
31 mins ago
Also note thatusleepunit is μs, so to wait 1 second, you need to provide a 1000000 argument.
– Ring Ø
27 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "2"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f469247%2fhow-do-i-sleep-for-a-millisecond-in-bash-or-ksh%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
$ 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 builtin sleep 0.1; done)
real 0m1.000s
user 0m0.004s
sys 0m0.004s
The downside is that the loadables may not be provided with your bash binary, so you would need to compile them yourself as shown (though on Solaris it would not necessarily be as simple as above).
As of bash-4.4 (September 2016) all the loadables are now built and installed by default on platforms that support it, though they are built as separate shared-object files, and without a .so suffix. Unless your distro/OS has done something creative, you should be able to do instead:
[ -z "$BASH_LOADABLES_PATH" ] &&
BASH_LOADABLES_PATH=$(pkg-config bash --variable=loadablesdir 2>/dev/null)
enable -f sleep sleep
(The man page implies BASH_LOADABLES_PATH is set automatically, I find this is not the case in the official distribution as of 4.4.12. If and when it is set correctly you need only enable -f filename commandname as required.)
If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature. The POSIX sleep command is minimal, older Solaris versions implemented only that. Solaris 11 sleep does support fractional seconds.
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
2
Ah, since you're usingexpectyou can probably just use "after N", where N is milliseconds, directly in your script.
– mr.spuratic
Jan 15 '13 at 14:46
useusleeplike @Luis Vazquez and @sebix write
– Ilan.K
Feb 20 '16 at 9:36
add a comment |
Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
$ 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 builtin sleep 0.1; done)
real 0m1.000s
user 0m0.004s
sys 0m0.004s
The downside is that the loadables may not be provided with your bash binary, so you would need to compile them yourself as shown (though on Solaris it would not necessarily be as simple as above).
As of bash-4.4 (September 2016) all the loadables are now built and installed by default on platforms that support it, though they are built as separate shared-object files, and without a .so suffix. Unless your distro/OS has done something creative, you should be able to do instead:
[ -z "$BASH_LOADABLES_PATH" ] &&
BASH_LOADABLES_PATH=$(pkg-config bash --variable=loadablesdir 2>/dev/null)
enable -f sleep sleep
(The man page implies BASH_LOADABLES_PATH is set automatically, I find this is not the case in the official distribution as of 4.4.12. If and when it is set correctly you need only enable -f filename commandname as required.)
If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature. The POSIX sleep command is minimal, older Solaris versions implemented only that. Solaris 11 sleep does support fractional seconds.
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
2
Ah, since you're usingexpectyou can probably just use "after N", where N is milliseconds, directly in your script.
– mr.spuratic
Jan 15 '13 at 14:46
useusleeplike @Luis Vazquez and @sebix write
– Ilan.K
Feb 20 '16 at 9:36
add a comment |
Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
$ 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 builtin sleep 0.1; done)
real 0m1.000s
user 0m0.004s
sys 0m0.004s
The downside is that the loadables may not be provided with your bash binary, so you would need to compile them yourself as shown (though on Solaris it would not necessarily be as simple as above).
As of bash-4.4 (September 2016) all the loadables are now built and installed by default on platforms that support it, though they are built as separate shared-object files, and without a .so suffix. Unless your distro/OS has done something creative, you should be able to do instead:
[ -z "$BASH_LOADABLES_PATH" ] &&
BASH_LOADABLES_PATH=$(pkg-config bash --variable=loadablesdir 2>/dev/null)
enable -f sleep sleep
(The man page implies BASH_LOADABLES_PATH is set automatically, I find this is not the case in the official distribution as of 4.4.12. If and when it is set correctly you need only enable -f filename commandname as required.)
If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature. The POSIX sleep command is minimal, older Solaris versions implemented only that. Solaris 11 sleep does support fractional seconds.
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
Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
$ 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 builtin sleep 0.1; done)
real 0m1.000s
user 0m0.004s
sys 0m0.004s
The downside is that the loadables may not be provided with your bash binary, so you would need to compile them yourself as shown (though on Solaris it would not necessarily be as simple as above).
As of bash-4.4 (September 2016) all the loadables are now built and installed by default on platforms that support it, though they are built as separate shared-object files, and without a .so suffix. Unless your distro/OS has done something creative, you should be able to do instead:
[ -z "$BASH_LOADABLES_PATH" ] &&
BASH_LOADABLES_PATH=$(pkg-config bash --variable=loadablesdir 2>/dev/null)
enable -f sleep sleep
(The man page implies BASH_LOADABLES_PATH is set automatically, I find this is not the case in the official distribution as of 4.4.12. If and when it is set correctly you need only enable -f filename commandname as required.)
If that's not suitable, the next easiest thing to do is build or obtain sleep from GNU coreutils, this supports the required feature. The POSIX sleep command is minimal, older Solaris versions implemented only that. Solaris 11 sleep does support fractional seconds.
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
edited Aug 21 '17 at 9:23
answered Jan 15 '13 at 13:52
mr.spuraticmr.spuratic
2,7001413
2,7001413
2
Ah, since you're usingexpectyou can probably just use "after N", where N is milliseconds, directly in your script.
– mr.spuratic
Jan 15 '13 at 14:46
useusleeplike @Luis Vazquez and @sebix write
– Ilan.K
Feb 20 '16 at 9:36
add a comment |
2
Ah, since you're usingexpectyou can probably just use "after N", where N is milliseconds, directly in your script.
– mr.spuratic
Jan 15 '13 at 14:46
useusleeplike @Luis Vazquez and @sebix write
– Ilan.K
Feb 20 '16 at 9:36
2
2
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
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
use
usleep like @Luis Vazquez and @sebix write– Ilan.K
Feb 20 '16 at 9:36
use
usleep like @Luis Vazquez and @sebix write– Ilan.K
Feb 20 '16 at 9:36
add a comment |
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.
1
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
|
show 1 more comment
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.
1
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
|
show 1 more comment
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.
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.
edited May 6 '14 at 8:52
Cristian Ciupitu
5,44013351
5,44013351
answered Jan 15 '13 at 13:22
scaiscai
1,6401815
1,6401815
1
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
|
show 1 more comment
1
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
1
1
see my remark about SOLARIS OS
– yael
Jan 15 '13 at 13:37
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
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
see my update in my quastion
– yael
Jan 15 '13 at 13:45
1
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
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
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
|
show 1 more comment
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
4
You can also drop the leading zero before the decimal point. eg.sleep .5
– Mike Causer
Jun 22 '14 at 7:13
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
add a comment |
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
4
You can also drop the leading zero before the decimal point. eg.sleep .5
– Mike Causer
Jun 22 '14 at 7:13
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
add a comment |
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
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
answered Jan 15 '13 at 13:24
colealtdeletecolealtdelete
5,37612434
5,37612434
4
You can also drop the leading zero before the decimal point. eg.sleep .5
– Mike Causer
Jun 22 '14 at 7:13
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
add a comment |
4
You can also drop the leading zero before the decimal point. eg.sleep .5
– Mike Causer
Jun 22 '14 at 7:13
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
4
4
You can also drop the leading zero before the decimal point. eg.
sleep .5– Mike Causer
Jun 22 '14 at 7:13
You can also drop the leading zero before the decimal point. eg.
sleep .5– Mike Causer
Jun 22 '14 at 7:13
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Except for mathforum.org/library/drmath/view/52352.html
– stark
Oct 27 '17 at 13:42
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
Talk about everyone else overcomplicating it...
– Martin
Sep 5 '18 at 7:45
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
@MikeCauser leading zeros much more readable and signal intent to the reader of the code later. also better when you actually do math.
– Alexander Mills
Dec 22 '18 at 6:51
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Apr 13 '17 at 12:14
Community♦
1
1
answered Jun 22 '14 at 19:52
dsrdakotadsrdakota
21122
21122
add a comment |
add a comment |
You may simply use usleep. It takes microseconds (= 1e-6 seconds) as parameter, so to sleep 1 millisecond you would enter:
usleep 1000
1
$ usleepNo command 'usleep' found, did you mean:Command 'sleep' from package 'coreutils' (main)usleep: command not found
– Bulletmagnet
Apr 5 '17 at 14:18
No, i meanusleeppart of theinitscriptspackage which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``
– Luis Vazquez
Jul 15 '17 at 17:14
1
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -sleep(from coreutils) works with seconds -usleep(from initscripts) works with micro-seconds
– Luis Vazquez
Jul 15 '17 at 17:22
add a comment |
You may simply use usleep. It takes microseconds (= 1e-6 seconds) as parameter, so to sleep 1 millisecond you would enter:
usleep 1000
1
$ usleepNo command 'usleep' found, did you mean:Command 'sleep' from package 'coreutils' (main)usleep: command not found
– Bulletmagnet
Apr 5 '17 at 14:18
No, i meanusleeppart of theinitscriptspackage which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``
– Luis Vazquez
Jul 15 '17 at 17:14
1
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -sleep(from coreutils) works with seconds -usleep(from initscripts) works with micro-seconds
– Luis Vazquez
Jul 15 '17 at 17:22
add a comment |
You may simply use usleep. It takes microseconds (= 1e-6 seconds) as parameter, so to sleep 1 millisecond you would enter:
usleep 1000
You may simply use usleep. It takes microseconds (= 1e-6 seconds) as parameter, so to sleep 1 millisecond you would enter:
usleep 1000
edited Jul 8 '15 at 20:01
sebix
3,57221738
3,57221738
answered Jul 8 '15 at 13:55
Luis VazquezLuis Vazquez
17014
17014
1
$ usleepNo command 'usleep' found, did you mean:Command 'sleep' from package 'coreutils' (main)usleep: command not found
– Bulletmagnet
Apr 5 '17 at 14:18
No, i meanusleeppart of theinitscriptspackage which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``
– Luis Vazquez
Jul 15 '17 at 17:14
1
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -sleep(from coreutils) works with seconds -usleep(from initscripts) works with micro-seconds
– Luis Vazquez
Jul 15 '17 at 17:22
add a comment |
1
$ usleepNo command 'usleep' found, did you mean:Command 'sleep' from package 'coreutils' (main)usleep: command not found
– Bulletmagnet
Apr 5 '17 at 14:18
No, i meanusleeppart of theinitscriptspackage which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``
– Luis Vazquez
Jul 15 '17 at 17:14
1
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -sleep(from coreutils) works with seconds -usleep(from initscripts) works with micro-seconds
– Luis Vazquez
Jul 15 '17 at 17:22
1
1
$ usleep No command 'usleep' found, did you mean: Command 'sleep' from package 'coreutils' (main) usleep: command not found– Bulletmagnet
Apr 5 '17 at 14:18
$ usleep No command 'usleep' found, did you mean: Command 'sleep' from package 'coreutils' (main) usleep: command not found– Bulletmagnet
Apr 5 '17 at 14:18
No, i mean
usleep part of the initscripts package which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``– Luis Vazquez
Jul 15 '17 at 17:14
No, i mean
usleep part of the initscripts package which is standard at least in all the Red Hat derived distributions; including at least RHEL, CentOS, Fedora, Mageia/Mandriva and SuSE. Here an example: `` ``– Luis Vazquez
Jul 15 '17 at 17:14
1
1
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -
sleep (from coreutils) works with seconds - usleep (from initscripts) works with micro-seconds– Luis Vazquez
Jul 15 '17 at 17:22
Here is a sample ilustration running in CentOS 7: ``` $ which usleep /usr/bin/usleep $ rpm -qf /usr/bin/usleep initscripts-9.49.37-1.el7_3.1.x86_64 ``` To summarize: -
sleep (from coreutils) works with seconds - usleep (from initscripts) works with micro-seconds– Luis Vazquez
Jul 15 '17 at 17:22
add a comment |
I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
#include "stdio.h"
int main(int argc, char **argv) {
if(argc == 2) { usleep(atoi(argv[1])); }
return 0;
}
Doesn't check arguments - I'd recommend a properly written one if you wanted to keep it but that (gcc usleep.c -o usleep) will get you out of a hole.
1
You could at least change that bareusleep()call toif(argc == 1) { usleep(atoi(argv[1])); }to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.
– a CVn
Oct 13 '16 at 15:05
@aCVn It's actuallyif (argc == 2) { usleep(atoi(argv[1])); }...
– Ring Ø
31 mins ago
Also note thatusleepunit is μs, so to wait 1 second, you need to provide a 1000000 argument.
– Ring Ø
27 mins ago
add a comment |
I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
#include "stdio.h"
int main(int argc, char **argv) {
if(argc == 2) { usleep(atoi(argv[1])); }
return 0;
}
Doesn't check arguments - I'd recommend a properly written one if you wanted to keep it but that (gcc usleep.c -o usleep) will get you out of a hole.
1
You could at least change that bareusleep()call toif(argc == 1) { usleep(atoi(argv[1])); }to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.
– a CVn
Oct 13 '16 at 15:05
@aCVn It's actuallyif (argc == 2) { usleep(atoi(argv[1])); }...
– Ring Ø
31 mins ago
Also note thatusleepunit is μs, so to wait 1 second, you need to provide a 1000000 argument.
– Ring Ø
27 mins ago
add a comment |
I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
#include "stdio.h"
int main(int argc, char **argv) {
if(argc == 2) { usleep(atoi(argv[1])); }
return 0;
}
Doesn't check arguments - I'd recommend a properly written one if you wanted to keep it but that (gcc usleep.c -o usleep) will get you out of a hole.
I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
#include "stdio.h"
int main(int argc, char **argv) {
if(argc == 2) { usleep(atoi(argv[1])); }
return 0;
}
Doesn't check arguments - I'd recommend a properly written one if you wanted to keep it but that (gcc usleep.c -o usleep) will get you out of a hole.
edited 27 mins ago
Ring Ø
3,89052745
3,89052745
answered May 24 '16 at 15:12
jrichemontjrichemont
412
412
1
You could at least change that bareusleep()call toif(argc == 1) { usleep(atoi(argv[1])); }to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.
– a CVn
Oct 13 '16 at 15:05
@aCVn It's actuallyif (argc == 2) { usleep(atoi(argv[1])); }...
– Ring Ø
31 mins ago
Also note thatusleepunit is μs, so to wait 1 second, you need to provide a 1000000 argument.
– Ring Ø
27 mins ago
add a comment |
1
You could at least change that bareusleep()call toif(argc == 1) { usleep(atoi(argv[1])); }to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.
– a CVn
Oct 13 '16 at 15:05
@aCVn It's actuallyif (argc == 2) { usleep(atoi(argv[1])); }...
– Ring Ø
31 mins ago
Also note thatusleepunit is μs, so to wait 1 second, you need to provide a 1000000 argument.
– Ring Ø
27 mins ago
1
1
You could at least change that bare
usleep() call to if(argc == 1) { usleep(atoi(argv[1])); } to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.– a CVn
Oct 13 '16 at 15:05
You could at least change that bare
usleep() call to if(argc == 1) { usleep(atoi(argv[1])); } to avoid indexing outside of the bounds of the array, which can lead to any number of unexpected behaviors.– a CVn
Oct 13 '16 at 15:05
@aCVn It's actually
if (argc == 2) { usleep(atoi(argv[1])); } ...– Ring Ø
31 mins ago
@aCVn It's actually
if (argc == 2) { usleep(atoi(argv[1])); } ...– Ring Ø
31 mins ago
Also note that
usleep unit is μs, so to wait 1 second, you need to provide a 1000000 argument.– Ring Ø
27 mins ago
Also note that
usleep unit is μs, so to wait 1 second, you need to provide a 1000000 argument.– Ring Ø
27 mins ago
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f469247%2fhow-do-i-sleep-for-a-millisecond-in-bash-or-ksh%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can I ask why you want to sleep for 1ms?
– Tom O'Connor
Jan 15 '13 at 14:03
1
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
2
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