{"id":26791,"date":"2018-05-28T02:17:10","date_gmt":"2018-05-28T09:17:10","guid":{"rendered":"https:\/\/linuxhint-com.zk153f8d-liquidwebsites.com\/?p=26791"},"modified":"2020-12-13T20:52:28","modified_gmt":"2020-12-14T04:52:28","slug":"sleep_command_linux","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/sleep_command_linux\/","title":{"rendered":"Sleep Command in Linux"},"content":{"rendered":"<div id=\"wpbody\">\nSleep command is used to delay for a fixed amount of time during the execution of any script. When the coder needs to pause the execution of any command for the particular purpose then this command is used with the particular time value. You can set the delay amount by <strong>seconds (s), minutes (m), hours (h) and days (d).<\/strong> \u00a0This tutorial will help you to learn the use of sleep command by using different bash scripts.<br \/>\n[adthrive-in-post-video-player video-id=&#8221;GHgdl1pR&#8221; upload-date=&#8221;2020-03-17T14:24:04.000Z&#8221; name=&#8221;Sleep command in linux&#8221; description=&#8221;Sleep command in linux&#8221; player-type=&#8221;collapse&#8221; override-embed=&#8221;true&#8221;]<\/p>\n<h2><strong>Sleep command syntax:<\/strong><\/h2>\n<p><strong>sleep number[suffix]<\/strong><\/p>\n<p>You can use any integer or fractional number as time value. Suffix part is optional for this command. If you omit suffix then time value is calculated as seconds by default. You can use <strong>s, <\/strong><strong>m, h<\/strong> and <strong>d <\/strong>as suffix value. The following examples show the use of sleep command with different suffixes.<\/p>\n<h3><strong>Example-1: sleep command without any suffix<\/strong><\/h3>\n<p>In the following script, sleep command is used with numeric value <strong>2<\/strong> only and no suffix is used. So, if you run the script then the string \u201c<strong>Task completed<\/strong>\u201d will print after waiting for 2 seconds.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n\u00a0<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Waiting for 2 seconds...&quot;<\/span><br \/>\n<span class=\"kw2\">sleep<\/span> <span class=\"nu0\">2<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Task Completed&quot;<\/span><\/div><\/div>\n<p>Run the bash file with <strong>time<\/strong> command to show the three types of time values to run the script. The output shows the time used by a system, user and real time.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw1\">time<\/span> <span class=\"kw2\">bash<\/span> sleep1.sh<\/div><\/div>\n<p class=\"Standard\"><b><span style=\"font-family: 'Ubuntu','serif'\">Output:<\/span><\/b><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-26793\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s-1.png\" alt=\"\" width=\"783\" height=\"156\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s-1.png 783w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s-1-300x60.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s-1-768x153.png 768w\" sizes=\"(max-width: 783px) 100vw, 783px\" \/><\/p>\n<h3><strong>Example-2: sleep command with a minute suffix<\/strong><\/h3>\n<p>In the following script, \u2018<strong>m<\/strong>&#8216; is used as the suffix with sleep command. Here, the time value is 0.05 minutes. After waiting 0.05 minutes, &#8220;<strong>Task completed<\/strong>\u201d message will be printed.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n\u00a0<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Waiting for 0.05 minutes...&quot;<\/span><br \/>\n<span class=\"kw2\">sleep<\/span> 0.05m<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Task Completed&quot;<\/span><\/div><\/div>\n<p>Run the script with <strong>time <\/strong>command like the first example.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw1\">time<\/span> <span class=\"kw2\">bash<\/span> sleep2.sh<\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-26794\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s1-1.png\" alt=\"\" width=\"781\" height=\"157\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s1-1.png 781w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s1-1-300x60.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s1-1-768x154.png 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/p>\n<h3><strong>Example-3: sleep command with hour suffix<\/strong><\/h3>\n<p>In the following script, \u2018<strong>h<\/strong>&#8216; is used as the suffix with sleep command. Here, the time value is 0.003 hour. After waiting 0.003 hour &#8220;<strong>Task completed<\/strong>&#8221; should be printed on the screen but it requires more times in reality when <strong>\u2018h\u2019<\/strong> suffix is used.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n\u00a0<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Waiting for 0.003 hours...&quot;<\/span><br \/>\n<span class=\"kw2\">sleep<\/span> 0.003h<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Task Completed&quot;<\/span><br \/>\n\u00a0<br \/>\n$ <span class=\"kw1\">time<\/span> <span class=\"kw2\">bash<\/span> sleep3.sh<\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-26795\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s3-1.png\" alt=\"\" width=\"782\" height=\"157\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s3-1.png 782w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s3-1-300x60.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s3-1-768x154.png 768w\" sizes=\"(max-width: 782px) 100vw, 782px\" \/><\/p>\n<h3><strong>Example-4: sleep command with loop<\/strong><\/h3>\n<p><strong>\u00a0<\/strong>You can use sleep command for various purposes. In the following example, sleep command is used with while loop. Initially, the value of the variable <strong>n<\/strong> is set to 1 and the value of <strong>n<\/strong> will be incremented by <strong>1<\/strong> for <strong>4<\/strong> times in every <strong>2<\/strong> seconds interval. So, when will you run the script, each output will appear after waiting\u00a0 2 seconds.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<span class=\"re2\">n<\/span>=<span class=\"nu0\">1<\/span><br \/>\n<span class=\"kw1\">while<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"re1\">$n<\/span> <span class=\"re5\">-lt<\/span> <span class=\"nu0\">5<\/span> <span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"kw1\">do<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;The value of n is now <span class=\"es2\">$n<\/span>&quot;<\/span><br \/>\n<span class=\"kw2\">sleep<\/span> 2s<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot; &quot;<\/span><br \/>\n<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span><span class=\"re2\">n<\/span>=<span class=\"re1\">$n<\/span>+<span class=\"nu0\">1<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw1\">done<\/span><\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-26796\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s4-1.png\" alt=\"\" width=\"783\" height=\"195\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s4-1.png 783w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s4-1-300x75.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s4-1-768x191.png 768w\" sizes=\"(max-width: 783px) 100vw, 783px\" \/><\/p>\n<h3><strong>Example-5: sleep command in the terminal with other commands<\/strong><\/h3>\n<p>Suppose, you want to run multiple commands and set the fixed time interval between the outputs of two commands, then you can use sleep command to do that task. In this example, the command <strong>ls<\/strong> and <strong>pwd<\/strong> are with <strong>sleep<\/strong> command. After executing the command, <strong>ls<\/strong> command will show the directory list of the current directory and show the current working directory path after waiting for 2 seconds.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw2\">ls<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw2\">sleep<\/span> <span class=\"nu0\">2<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">pwd<\/span><\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-26797\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s5-1.png\" alt=\"\" width=\"785\" height=\"454\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s5-1.png 785w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s5-1-300x174.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s5-1-768x444.png 768w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><\/p>\n<h3><strong>Example-6: Using sleep command from the command prompt<\/strong><\/h3>\n<p>sleep command is used between two echo commands in the following example. Three time values will be displayed after executing the command.<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"bash codecolorer\"><span class=\"co4\">$ <\/span><span class=\"kw1\">time<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Start&quot;<\/span>; <span class=\"kw2\">sleep<\/span> <span class=\"nu0\">5<\/span>; <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;End&quot;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-26798\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s6-1.png\" alt=\"\" width=\"731\" height=\"157\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s6-1.png 731w, https:\/\/linuxhint.com\/wp-content\/uploads\/2018\/05\/s6-1-300x64.png 300w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/p>\n<p class=\"Standard\"><span style=\"font-family: 'Ubuntu','serif'\">sleep command is a useful command when you need to write a bash script with multiple commands or tasks, the output of any command may require a large amount of time and other command need to wait for completing the task of the previous command. For example, you want to download sequential files and next download can&#8217;t be started before completing the previous download. In this case, it is better to sleep command before each download to wait for the fixed amount of time.<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sleep command is used to delay for a fixed amount of time during the execution of any script. When the coder needs to pause the execution of any command for the particular purpose then this command is used with the particular time value. You can set the delay amount by seconds (s), minutes (m), hours [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":26792,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"0","tag":"0","restrict_content":"0"},"footnotes":""},"categories":[2161],"tags":[],"class_list":["post-26791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-commands"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/26791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=26791"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/26791\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/26792"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=26791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=26791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=26791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}