{"id":1901,"date":"2020-11-30T22:15:39","date_gmt":"2020-11-30T22:15:39","guid":{"rendered":"https:\/\/linuxdigest.com\/?p=1901"},"modified":"2020-12-13T17:16:10","modified_gmt":"2020-12-13T17:16:10","slug":"make-bash-sleep-for-less-than-a-second","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/","title":{"rendered":"Make bash sleep for less than a second"},"content":{"rendered":"\n

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<\/p>\n\n\n\n

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.<\/strong><\/p>\n\n\n\n

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.<\/p>\n\n\n\n

Sleep for half a minute<\/h4>\n\n\n\n
sleep 0.5<\/code><\/pre>\n\n\n\n

Sleep for one tenth of a second<\/h4>\n\n\n\n
sleep 0.1<\/code><\/pre>\n\n\n\n

Sleep for a millisecond<\/h4>\n\n\n\n
sleep 0.001<\/code><\/pre>\n\n\n\n

Sleep using scientific e notation<\/h4>\n\n\n\n

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).<\/p>\n\n\n\n

sleep 1e-3<\/code><\/pre>\n\n\n\n

Sleep using arithmetictic operations<\/h4>\n\n\n\n

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.<\/p>\n\n\n\n

sleep $(( 1\/5 ))<\/code><\/pre>\n\n\n\n

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”.<\/p>\n\n\n\n

Period<\/strong><\/td>Suffix<\/strong><\/td><\/tr>
Seconds<\/td>s<\/td><\/tr>
Minutes<\/td>m<\/td><\/tr>
Hours<\/td>h<\/td><\/tr>
Days<\/td>d<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

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!<\/p>\n\n\n\n

Sleep for one year<\/h4>\n\n\n\n
sleep 365d<\/code><\/pre>\n\n\n\n

This would make your script pause for a whole year. Assuming it is not a leep year.<\/p>\n\n\n\n

Use usleep as an alternative<\/h2>\n\n\n\n

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.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

Use usleep to sleep for half a second (half a million microseconds):<\/h4>\n\n\n\n
usleep 500000<\/code><\/pre>\n\n\n\n

Use usleep to sleep for one millisecond (one thousand microseconds):<\/h4>\n\n\n\n
usleep 1000<\/code><\/pre>\n\n\n\n

Use pythons sleep function<\/h2>\n\n\n\n

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.<\/p>\n\n\n\n

Sleep using python instead of bash<\/h4>\n\n\n\n
python -c 'import time; time.sleep(0.5)'<\/code><\/pre>\n\n\n\n

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.<\/p>\n\n\n\n

Conclusion<\/h2>\n\n\n\n

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.<\/p>\n","protected":false},"excerpt":{"rendered":"

For the longest time, I assumed the sleep command only accepted whole integers as an argument.…<\/p>\n","protected":false},"author":1,"featured_media":1920,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[36],"tags":[],"yoast_head":"\nMake bash sleep for less than a second - Linux Digest<\/title>\n<meta name=\"description\" content=\"For the longest time, I assumed the sleep command only accepted whole integers as an argument. Turns out sleep can pause for less than a second.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Make bash sleep for less than a second - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"For the longest time, I assumed the sleep command only accepted whole integers as an argument. Turns out sleep can pause for less than a second.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-30T22:15:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-13T17:16:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"Make bash sleep for less than a second\",\"datePublished\":\"2020-11-30T22:15:39+00:00\",\"dateModified\":\"2020-12-13T17:16:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\"},\"wordCount\":594,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"articleSection\":[\"Bash scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\",\"name\":\"Make bash sleep for less than a second - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2020-11-30T22:15:39+00:00\",\"dateModified\":\"2020-12-13T17:16:10+00:00\",\"description\":\"For the longest time, I assumed the sleep command only accepted whole integers as an argument. Turns out sleep can pause for less than a second.\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Make bash sleep for less than a second\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Make bash sleep for less than a second - Linux Digest","description":"For the longest time, I assumed the sleep command only accepted whole integers as an argument. Turns out sleep can pause for less than a second.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/","og_locale":"en_US","og_type":"article","og_title":"Make bash sleep for less than a second - Linux Digest","og_description":"For the longest time, I assumed the sleep command only accepted whole integers as an argument. Turns out sleep can pause for less than a second.","og_url":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/","og_site_name":"Linux Digest","article_published_time":"2020-11-30T22:15:39+00:00","article_modified_time":"2020-12-13T17:16:10+00:00","og_image":[{"width":900,"height":600,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/Copy-of-Untitled-1.jpg","type":"image\/jpeg"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"Make bash sleep for less than a second","datePublished":"2020-11-30T22:15:39+00:00","dateModified":"2020-12-13T17:16:10+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/"},"wordCount":594,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"articleSection":["Bash scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/","url":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/","name":"Make bash sleep for less than a second - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2020-11-30T22:15:39+00:00","dateModified":"2020-12-13T17:16:10+00:00","description":"For the longest time, I assumed the sleep command only accepted whole integers as an argument. Turns out sleep can pause for less than a second.","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/make-bash-sleep-for-less-than-a-second\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"Make bash sleep for less than a second"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1901"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1901"}],"version-history":[{"count":3,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1901\/revisions"}],"predecessor-version":[{"id":1904,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1901\/revisions\/1904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1920"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}