Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'sleep' Command In Linux

2023-05-23 - By Robert Elder

     I use the 'sleep' command to delay for a specified amount of time:

sleep 123

     If I run the 'sleep' command with a value of 1, it will do absolutely nothing for one second and then return control to the prompt:

sleep 1

     Many versions of the 'sleep' command will allow you to specify a time as a floating point number as well as an integer:

sleep 0.3
sleep 1.3

Sleep Using Units - Seconds, Minutes, Hours, Days

     Some versions of the 'sleep' command will allow you to specify a suffix after the number to control whether the 'sleep' time indicates seconds, minutes, hours, or days:

sleep 1.3m

     You can consult the 'info' pages of the sleep command for a list of suffixes:

info sleep
25.1 ‘sleep’: Delay for a specified time
========================================

‘sleep’ pauses for an amount of time specified by the sum of the values
of the command line arguments.  Synopsis:

     sleep NUMBER[smhd]...

   Each argument is a number followed by an optional unit; the default
is seconds.  The units are:

‘s’
     seconds
‘m’
     minutes
‘h’
     hours
‘d’
     days

Using 'sleep' To Lower CPU Usage In Busy Loops

     The 'sleep' command is useful for scripting because it lets you delay program execution without wasting CPU usage inside of a busy loop.  If I run this command in a busy loop:

for i in {1..10000000}; do :; done

     it will also cause a delay just like the 'sleep' command does, but it also consumes 100% of the CPU:

Busy Loop Without Sleep

     The 'sleep' command allows for delays without wasting CPU by taking advantage of blocking features within the operating system scheduler.  If I run this command instead:

while true; do :; sleep 1; done

     you can see that the CPU usage is now much lower:

Busy Loop With Sleep

     And that's why the 'sleep' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use