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.XXX
works fine , but on solarissleep 0.1
orsleep 0.01
- illegal syntax
The documentation for the
sleep
command from coreutils says:Hence you can use
sleep 0.1
,sleep 1.0e-1
and similar arguments.Bash has a "loadable" sleep which supports fractional seconds, and eliminates overheads of an external command:
Then:
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 buildbash-4.4
with loadable extensions deliberately removed), you should be able to do instead:(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 onlyenable -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 POSIXsleep
command is minimal, older Solaris versions implemented only that. Solaris 11sleep
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:Sleep accepts decimal numbers so you can break it down this like:
1/2 of a second
1/100 of a second
So for a millisecond you would want
Try this to determine accuracy:
Combination of mr.spuratic's solution and coles's solution.
You may simply use
usleep
. It takes microseconds (= 1e-6 seconds) as parameter, so to sleep 1 millisecond you would enter:I had the same problem (no shell usleep on Solaris) so I wrote my own thus:
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 like the usleep idea, but I can't make a comment under it. Since this helped me out, I hope my suggestion can improve the usleep idea.
https://github.com/fedora-sysv/initscripts/blob/3c3fe4a4d1b2a1113ed302df3ac9866ded51b01b/src/usleep.c is the actual source code for usleep.c on the redhat ecosystem.
Try to compile that in your Solaris. You'd probably need https://www.opencsw.org/packages/libpopt0/.
The POSIX specification for
sleep
only accepts an integral argument -- so no fractions of a second. GNU's coreutilssleep
adds support for real numbers, suffixes, even scientific notation andinf
inity as GNU extensions. But if you're on embedded system withbusybox
or just don't have coreutils, then you're out of luck unless you have perl.