Using the sleep Command in Linux

In this tutorial, we will be showing you how the sleep command works in Linux

sleep command on Linux

The sleep command in Linux allows you to temporarily pause the shell, waiting for a specified amount of time before executing the next command.

This command is most useful when it is utilized within a bash script. Sleep will allow you to pause the execution of the script for a specific amount of time.

For example, if you were using a script to download files from a website, you can use Linux’s sleep command to pause execution between each download.

A script can be slept from any time, from a few milliseconds, up to days.

Over the following few sections, we will show how you can use the sleep command to pause the execution of the terminal or a script.

Syntax of the sleep Command in Linux

Let us start by exploring the syntax of the sleep command in Linux. It is a reasonabily straightforward command that only has one parameter, though that parameter can be specified multiple times.

Below you can see the syntax for this command. It’s fairly simple, so it should be easy to remember for future use.

sleep NUMBER[smhd]...

The number parameter is where you will specify the amount of time you want the script to sleep. This can either be a whole number (1) or a floating- point (1.25).

Additionally, you can use one of the unit suffixes specified below to change the value specified below.

  • s – The specified number is in seconds. (Seconds is the default value)
  • m – The value you have specified is in minutes.
  • h – The sleep time is defined as hours.
  • d – The final suffix you can use specifies that your number is in “days“.

As the sleep command supports multiple time arguments, you can chain multiple values together. For example, you can use “1m 15s” to pause the execution of a script in Linux for 1 minute and 15 seconds.

Examples of using the Sleep command on Linux

Now that we know the syntax of this tool let us show you some examples of using the sleep command on Linux.

You will quickly see that this is a relatively straightforward command to use on your Linux system.

Using the sleep Command to Pause for Seconds

For our first example, we will use the sleep command to pause the current terminal for 15 seconds.

All we need to do is use “sleep” followed by the number 15, as shown in the example command below

sleep 15

Additionally, if you use a system that supports suffixes, you can also use “s” after the number to specify seconds.

sleep 15s

To showcase how this causes the current session to go to sleep, we can use the “&&” symbol to run a command immediately after the previous one finishes running.

After the Linux sleep command has finished waiting for 15 seconds, the terminal will continue to run again, printing the message “Slept for 15 seconds“.

sleep 15 && echo "Slept for 15 seconds"

Pausing Execution for Minutes using the sleep Command on Linux

To specify the sleep time in Linux with this command, you will need to use the “m” suffix. This suffix will tell the sleep command that the number should be regarded as minutes.

Below we will walk you through a couple of quick examples of how you can set the sleep time in minutes.

For this first example, let us use sleep to pause execution for 5 minutes. You can see that we used “5” followed immediately by the “m” syntax.

sleep 5m

With the command below, the Linux terminal will sleep for 5 minutes before the message “Slept for 5 minutes” will be printed.

sleep 5m && echo "Slept for 5 minutes"

Sleeping the Terminal for Hours

The sleep command also allows you to sleep the script for hours by utilizing the “h” suffix. By utilizing “h” after your number, the command will interpret what you wrote as hours.

To showcase the use of this suffix, we will go through two short examples.

For this first example, we will use Linux’s sleep command to pause the terminal for 1 hour.

sleep 1h

To showcase this in practice, you can try the following command. The execution will be paused for an hour before the echo command will print the message “Slept for 1 hour“.

sleep 1h && echo "Slept for 1 hour"

Pause Script Execution using the Day Suffix

The last suffix that Linux’s sleep command supports is the day suffix. You can specify the number as days by using the “d” suffix”.

In the following example, we use the “d” suffix to sleep the session for 2 days.

sleep 2d

If you want to test this, you can use the following command. With this command, we sleep for 2 days, after which the text “Slept for 2 days” will be printed.

sleep 2d && echo "Slept for 2 Days"

Using Multiple Sleep Times with the sleep Command on Linux

Linux’s sleep command supports multiple numbers to calculate how long it should sleep. So, for example, you could sleep a script for 1 day, 20 minutes, and 10 seconds.

All you need to do is specify the time one after another.

To showcase this behavior, you can check out the following example. With this example, we sleep the script for 1 hour, 10 minutes, and 10 seconds.

While we use the “s” suffix for our seconds in this example, it is not required.

sleep 1h 10m 10s

Using a Floating Point Number with the sleep command

The sleep command supports using a floating-point number. Using this, you can specify partial numbers to sleep for half a second, one and a half minutes.

To showcase this, let us show a couple of examples of how you can use this.

Let us start by sleeping the terminal for half a second. This requires us to use “0.5” in the number field, as shown below.

sleep 0.5

You can also use floating-point numbers for other units, such as minutes. For example, to sleep for one and a half minutes, you can utilize the following command.

sleep 1.5m

Conclusion

In this guide, we have shown you how you can use the sleep command on a Linux-based system.

This command allows you to pause the execution of the terminal or script. It is useful when performing tasks where you need to wait before proceeding.

If you have any questions about using the sleep tool, please comment below.

To learn more about Linux, check out our many other Linux command guides.

Leave a Reply

Your email address will not be published. Required fields are marked *