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
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
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
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
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 w3wiki!"
sleep 5
echo "Sleep for 5 seconds"
In this, we created the script using `vim`, then made our script executable `chmod +x`, then ran our script using `./example.sh`.
sleep Command in Linux with Examples
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.