Open In App

sleep Command in Linux with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 04 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads