Home Bash scripting Make bash sleep for less than a second

Make bash sleep for less than a second

by The Linux Digest Guy

For the longest time, I assumed the sleep command only accepted whole integers as an argument. When I was trying to find a way to make a script pause for less than one second, I found that this is not true. That was silly of me. But, I assume, there are more people like me, that make this assumption. So here is a little writeup about how you can make bash sleep for half a second or less

Although some, mostly older, implementations of sleep only accept whole minutes as arguments, GNU sleep accepts arbitrary floating point values as well. So “sleep 0.5” will make the script pause for half a second.

I don’t know why I assumed this was not possible. But for future reference, I have included some examples of how you can use different values for sleep. There are also some alternative methods at the bottom if your version of sleep only supports whole minutes.

Sleep for half a minute

sleep 0.5

Sleep for one tenth of a second

sleep 0.1

Sleep for a millisecond

sleep 0.001

Sleep using scientific e notation

Unbelievably, sleep also accepts scientific e notation. This could be useful if you need to pass very small values. In the example, here below, we tell sleep to pause for 0.001 seconds (millisecond).

sleep 1e-3

Sleep using arithmetictic operations

If you want you can always have bash calculate the value for you as well. Like in this case we want to have the script pause for one-fifth of a second.

sleep $(( 1/5 ))

For longer periods, you can also use suffixes to make the commands shorter. Otherwise, you would have to calculate how many seconds there are in the time you want. Like 600 seconds for 10 minutes. Instead, you can just write “sleep 10m” or “sleep 1d” for one day instead of “sleep 86400”.

PeriodSuffix
Secondss
Minutesm
Hoursh
Daysd

So you could write something like this if you wanted to pause for one year. I have a hard time thinking of reasons why you would want to do this. But you never know!

Sleep for one year

sleep 365d

This would make your script pause for a whole year. Assuming it is not a leep year.

Use usleep as an alternative

If your version of sleep only accepts whole integers as arguments, you can also use usleep. That is if that is available on your system. Not all *nix systems have usleep by default.

usleep takes microseconds as arguments instead of seconds. The default value, if you don’t specify any argument is 1 microsecond. Here are a couple of examples.

Use usleep to sleep for half a second (half a million microseconds):

usleep 500000

Use usleep to sleep for one millisecond (one thousand microseconds):

usleep 1000

Use pythons sleep function

If all else fails you could use call some other scripting language that accepts floating point numbers as arguments. Python is one such language. You need to import the time module to get access to the sleep function. But this can all be done on one line.

Sleep using python instead of bash

python -c 'import time; time.sleep(0.5)'

Of course, there are many other scripting languages you could use. Some of them, like Perl, need to have an extra dependency installed to be able to sleep for less than a whole second.

Conclusion

If you have GNU sleep on your system, you can easily pass values smaller than one. Thereby pausing your script for less than a second. If you are running some other Unix operating system other than Linux, you have some other alternatives like usleep or another scripting language like python.

3

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy