Master the Linux ‘sleep’ Command: A Comprehensive Guide

Peter Hou
3 min readMay 27, 2023

--

This guide provides a comprehensive overview of the ‘sleep’ command in Linux. It covers the history of the command, how to use it, the most commonly used parameters, and some lesser-known tricks. ‘sleep’ is a valuable tool for system administrators as it allows them to pause the execution of a script or command for a specified amount of time. This guide includes examples and common use cases, making it an excellent resource for beginners looking to enhance their Linux skills.

Instructions

This guide provides a comprehensive overview of the ‘sleep’ command in Linux. It covers the history of the command, how to use it, the most commonly used parameters, and some lesser-known tricks. ‘sleep’ is a valuable tool for system administrators as it allows them to pause the execution of a script or command for a specified amount of time. This guide includes examples and common use cases, making it an excellent resource for beginners looking to enhance their Linux skills.

History

The ‘sleep’ command has been a part of Unix-like operating systems for many years. It was introduced to provide a way for system administrators and programmers to delay the execution of a script or command.

When and why to use it

You would use the ‘sleep’ command when you want to pause the execution of a script or command for a specific amount of time. This is particularly useful in scripting and programming, where you may need to wait for a certain condition to be met before proceeding.

How to use it

The basic syntax of the ‘sleep’ command is sleep NUMBER[SUFFIX]. You specify the amount of time you want the command or script to pause.

$ sleep 5s

The commonly used parameters

The ‘sleep’ command does not have parameters in the traditional sense. Instead, it takes a single argument: the duration of the sleep. This duration can be specified in seconds (s), minutes (m), hours (h), or days (d).

  • 5s This would pause the script or command for 5 seconds.
$ sleep 5s
  • 10m This would pause the script or command for 10 minutes.
$ sleep 10m

Other supported parameters

As mentioned above, the ‘sleep’ command does not have traditional parameters. The duration of the sleep can be specified in seconds (s), minutes (m), hours (h), or days (d).

Most common use cases

One of the most common use cases for the ‘sleep’ command is in scripting. For example, you might have a script that checks the status of a particular service every 5 minutes.

$ while true; do
> check_service_status
> sleep 5m
> done

The tricky skills

While ‘sleep’ is a fairly straightforward command, one lesser-known trick is that you can use fractions of seconds for the sleep duration.

$ sleep 0.5s

What needs to be noted

When using the ‘sleep’ command, it’s important to remember that the command or script will be paused for the entire duration of the sleep. If you need the script to continue executing in the background, you may need to use other methods.

Conclusion

The ‘sleep’ command is a powerful tool in the Linux system administrator’s toolbox. It allows for precise control over the execution of scripts and commands, making it invaluable in many scripting and programming scenarios.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet