- How do I sleep for a millisecond in bash or ksh
- 8 Answers 8
- Using Linux Sleep Command in Bash Scripts
- Bash sleep command Examples
- Sleep command without suffix counts in seconds
- Sleep command with minute or hour or day suffix
- Sleep command with a combination of second, minute, hour and day
- Bonus Tip: Sleep less than a second
- How do I sleep for a millisecond in bash or ksh
- Find below all possible solutions or suggestions for the above questions..
How do I sleep for a millisecond in bash or ksh
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
Whatever solution you choose, keep in mind that a shell script won’t be very accurate in terms of timing.
How about doing something that takes a very short time to execute, but does nothing.. like echo «» >/dev/null
8 Answers 8
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
$ 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 (sadly RHEL/CentOS 8 build bash-4.4 with loadable extensions deliberately removed), 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
Using Linux Sleep Command in Bash Scripts
This tutorial shows you how to use sleep commands and its various options in bash scripts.
Linux sleep command is one of the simplest commands out there. As you can guess from the name, its only function is to sleep. In other words, it introduces a delay for a specified time.
So, if you use the sleep command with x and the next command can only be run after x seconds.
Sleep command has a simple syntax:
In here, the suffix could be:
- s for seconds. This is the default.
- m for minutes.
- h for hours.
- d for days.
Let’s see some examples of the sleep command.
Bash sleep command Examples
Though you can use it in a shell directly, the sleep command is commonly used to introduce a delay in the execution of a bash script. I am going to show the usage of sleep command through sample bash scripts.
Sleep command without suffix counts in seconds
Suppose you want pause your bash script for 5 seconds, you can use sleep like this:
In a sample bash script, it could look like this:
!/bin/bash echo "Sleeping for 5 seconds…" sleep 5 echo "Completed"
If you run it with the time command, you’ll see that the bash script actually ran for (a slightly) more than 5 seconds.
time ./sleep.sh Sleeping for 5 seconds… Completed real 0m5.008s user 0m0.000s sys 0m0.007s
Sleep command with minute or hour or day suffix
You can specify the sleep time in minutes in the following way:
This will pause the script/shell for one minute. If you want to delay the script in hours, you can do that with the h option:
Even if you want to pause the bash script for days, you can do that with the d suffix:
This could help if you want to run on alternate days or week days.
Sleep command with a combination of second, minute, hour and day
You are not obliged to use only one suffix at a time. You can use more than one suffix and the duration of the sleep is the sum of all the suffix.
For example, if you use the follow command:
This will keep the script waiting for 1 hour, 10 minutes and 5 seconds. Note that the s suffix is still optional here.
Bonus Tip: Sleep less than a second
You might have noticed that the smallest unit of time in the sleep command is second. But what if your bash script to sleep for milliseconds?
The good thing is that you can use floating point (decimal points) with sleep command.
So if you want to introduce a 5 milliseconds pause, use it like this:
You can also use decimal points with other suffixes.
It will introduce a delay of 1 hour, 37 minutes and 30 seconds.
I hope you didn’t sleep while reading these examples of sleep command 😉
If you are interested in shell scripting, perhaps you would like reading about string comparison in bash as well. If you have questions or suggestions, please feel free to ask.
How do I sleep for a millisecond in bash or 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 ?
Find below all possible solutions or suggestions for the above questions..
Suggestion: 1:
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
$ 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