sleep Command in Linux with Examples
Last Updated :
19 Jul, 2024
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
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
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
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
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
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.
Similar Reads
scriptreplay command in Linux with Examples scriptreplay command is used to replay a typescript/terminal_activity stored in the log file that was recorded by the script command. With the help of timing information, the log files are played and the outputs are generated in the terminal with the same speed the original script was recorded. The
3 min read
sdiff command in Linux with Examples sdiff command in Linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays a greater than sign if the line only exists in the file spe
3 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
select command in Linux with examples select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of commands written in the select block and then asks again to enter a number, if a wrong option is entered it does nothing. If the user enters
2 min read
seq command in Linux with Examples The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to a
4 min read
setsid command in Linux with Examples setsid command in Linux system is used to run a program in a new session. The command will call the fork(2) if already a process group leader. Else, it will execute a program in the current process. The main advantage of using 'setsid' is that it allows programs to run independently of the terminal
3 min read
Shift command in Linux with examples Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command
3 min read
showkey command in Linux with Examples showkey command in Linux is used to examine the codes sent by the keyboard. showkey prints to standard output either the scan codes or the key code or the 'ASCII' code of each key pressed. The program runs in the first two modes until 10 seconds have elapsed since the last key press or release event
2 min read
shred Command in Linux with Examples When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your
6 min read
shutdown command in Linux with Examples The `shutdown` operation in Linux is a crucial command for managing system power states, allowing administrators to halt safely, power off, or reboot the system. This command provides flexibility in scheduling downtimes, ensuring minimal disruption to users and processes. In this article, we will ex
5 min read