Open In App

sleep Command in Linux with Examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

sleep command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER.

Syntax of `sleep` Command in Linux

sleep NUMBER[SUFFIX]...

Here,

“NUMBER” represents the time duration for which the command should sleep.

“SUFFIX” can be used to specify the unit of time (s for seconds, m for minutes, h for hours, etc.).

Note: If no suffix is provided, the default unit is seconds.

Options Available in sleep command:

`–help` Option in Sleep Command

It displays help information about sleep commands

`–version` Option in Sleep Command

It displays version information of the sleep command.

Examples of `sleep` Command in Linux

Basic Usage of `sleep` Command in Linux:

The simplest use of the Sleep command involves specifying the duration in seconds. For instance:

sleep 6

Basic Usage of sleep command

Basic Usage of Sleep Command

In this example, the Sleep command pauses the execution for 6 seconds. This is particularly useful in scripts where you need to introduce delays between commands or processes.

Using Suffixes in `sleep` Command in Linux:

Sleep allows you to specify time units using suffixes, providing flexibility in defining durations. Here’s an example:

sleep 3m

Suffix `m` in Sleep Command

Suffix `m` in Sleep Command

In this case, the ‘m’ suffix denotes minutes. Therefore, the Sleep command will pause execution for 3 minutes. This is beneficial when you need more extended periods of delay, and using suffixes makes the command more human-readable.

Other suffixes include:

Suddixes

Description

‘s’

This is used for specifying seconds.

‘h’

This is used for specifying hours.

‘d’

This is used for specifying days.

For instance:

sleep 3h

This command sleeps for 3 hours.

Specifying Fractional Seconds in `sleep` Command in Linux

You can use decimal values to specify fractional seconds.

sleep 3.5

Fractional Seconds in `sleep` Command

Fractional Seconds in `Sleep` Command

This command sleeps for 3.5 seconds, allowing for more precise control over the sleep duration.

Interrupting Sleep:

The Sleep command can be interrupted using signals, such as pressing `Ctrl+C`, which sends a SIGINT signal, terminating the sleep:

sleep 12
# Press Ctrl+C after a few seconds to interrupt the sleep

Interrupting Sleep

Interrupting Sleep

This allows users to gracefully interrupt the sleep duration and proceed with other actions.

Bash Sleep Command

The Sleep command is like a pause button for your computer. You just type “sleep N,” with N being a number (either a whole number or a number with decimals), and it makes your computer wait for that many seconds before doing the next thing in your script. It’s that easy!

Let us understand this with an Example of a script.

We have this script :

#!/bin/bash
echo "Hello GeeksforGeeks!"
sleep 5
echo "Sleep for 5 seconds"

Bash Sleep Command

Bash Sleep Command

In this, we created the script using `vim`, then made our script executable `chmod +x`, then ran our script using `./example.sh`.

Conclusion

In this article, we discussed the `sleep` command in Linux which is a versatile tool for introducing delays in script execution. Its simple syntax, defined as `sleep NUMBER[SUFFIX]...`, allows users to easily specify time durations, either in seconds or with various suffixes denoting minutes, hours, or days. This article covered basic usage with examples like `sleep 6`, demonstrated the use of suffixes as in `sleep 3m`, and explored advanced options such as interrupting sleep with signals. The Bash `Sleep` Command serves as a pause button, enabling computers to wait for a specified duration before proceeding to the next task in a script. Overall, the sleep command proves essential in scripting, providing precise control over time delays and enhancing overall efficiency in Linux operations.

sleep Command in Linux with Examples – FAQs

What is the sleep command used for in Linux?

The sleep command in Linux is used to delay for a specified amount of time during the execution of a script or a command. It is a simple utility that suspends the execution of the command-line or script for a given period, defined in seconds by default, but can also handle minutes, hours, and even days.

How to pause a script for a specific amount of time using sleep?

To pause a script for a specific amount of time using sleep, you specify the number of seconds as an argument. For example, to pause a script for 5 seconds, you would use:

sleep 5

If you need to pause for a more specific duration, such as minutes or hours, you can use different suffixes: m for minutes, h for hours, and d for days. For example, to sleep for 2 minutes:

sleep 2m

Can sleep be used for milliseconds or microseconds?

On most modern systems, the sleep command can also handle sub-second delays using decimal numbers to specify fractions of a second. For example, to pause for half a second, you can use:

sleep 0.5

However, sleep does not natively handle microseconds directly. If you need such precision, you might need to look at other programming options like using Python or Perl scripts where sleep functions can handle smaller time units.

How to use sleep in a loop?

Using sleep in a loop is a common method for creating a delay between executions of a loop body. Here’s an example using a bash loop that executes a command every 5 seconds, repeating 10 times:

for i in {1..10}
do
echo "Repeat $i"
sleep 5
done

This script will print “Repeat” followed by the iteration number every 5 seconds.

What are some common use cases for the sleep command?

The sleep command has a variety of use cases in system administration and scripting:

  1. Batch Processing: Delaying the execution between batch jobs to reduce load on system resources.
  2. Polling: Frequently checking the status of a file or process at set intervals.
  3. Scheduling Tasks: Although not a replacement for cron jobs, sleep can be used in scripts to delay actions or manage timing manually.
  4. Testing: Introducing delays in scripts to test timeouts and asynchronous behaviors in development environments.
  5. Throttling: Slowing down a loop’s execution to prevent overloading a service or network during API calls or data scraping.


Next Article

Similar Reads

three90RightbarBannerImg