Updated June 30, 2023
Introduction to Linux Sleep
In the Linux ecosystem, the sleep command is used to hold the execution of a job or command for a certain amount of time. The number value defines the time value. In other words, creating a mock job in the Linux environment is also useful. It can be used in the live shell window as well as in the shell or bash jobs, or it can be used in a combination of both.
Jim Meyering and Paul Eggert wrote the sleep command utility.
Note: By default, the sleep value is one second. But we can use the suffix values to define the sleep value for a specific time interval.
Syntax:
sleep NUMBER[SUFFIX] ...
Sleep OPTION is given below:
- sleep: We can use the sleep keyword in the syntax or command. It will accept the arguments like the number value or the suffix value. The sleep command will help to pause the execution of any shell, bash job, or command on the Linux environment.
- NUMBER: We can provide the numeric value in the sleep command. The same numeric value will be used to pause the command or job on the Linux shell for a certain time frame.
- SUFFIX: In the sleep command, we can use the suffix values to reduce the number of calculations for converting the time values into a second value.
How Linux Sleep Command Works?
Linux has different ways to pause or hold commands or jobs. The sleep command is one of the ways to pause the execution of the jobs or commands for a specific amount of time. In the older Linux versions, using the sleep command with a floating number is no functional. But in modern Linux, there is functionality to use the floating number values with the sleep command.
The sleep command will accept the numeric value. The same numeric value will be used to pause the next scheduled job or the next scheduled command to run. We have the functionality to use the SUFFIX values with the sleep command. Below are the SUFFIX values that can be used in the sleep command.
- s: The “s” suffix value stands for the second.
- m: The “m” suffix value stands for the minute.
- h: The “h” suffix value stands for the hour.
- d: The “d” suffix value stands for the day.
Examples to Implement Linux Sleep Command
In the sleep command, we can pause the shell windows for a specific period. By default, the specific time is in seconds.
Example #1
Command:
sleep 5
Explanation: As per the above sleep command, we use the “5” value. It is mandatory to use any value with the sleep command; otherwise, the command will not work. Once the command hits, the shell windows are stuck for 5 seconds. After 5 seconds, the shell window will be normal and able to execute the normal Linux queries.
Output:
Example #2 – With Suffix Value
In the Linux environment, we can use the suffix values in the sleep command.
Command:
sleep 5s
sleep 1m
Explanation: As per the requirement, we can define the suffix values in the sleep command. As per the below screenshot 2 (a), we have used the “s” (second) suffix with the sleep command. Similarly, we can use the “m” (minute) suffix. So it will pause the screen for 1min(refer to screenshot 2 (a)).
Output :
Example #3 – With Linux Command
In the Linux environment, we can also use the sleep command with the Linux command.
Command:
sleep 5s && cat test_job.sh
Explanation: As per the above command, we use the sleep command before the Linux command. The execution of the command is held for 5 seconds (refer to screenshot 3 (a)). After that, the command will be executed (refer to screenshot 3 (b)).
Output:
Example #4 – With Bash Job
In the Linux ecosystem, we also have the functionality to use the sheep command in bash jobs.
Command:
./test_job.sh
Shell Job:
#!/usr/bin/bash
sleep 5s
echo "Hello"
Explanation: As per the above bash job, we have used the sleep command. While running the job, first, it will pause the execution for 5second (refer to screenshot 4 (a)). After that, the echo command will execute (refer to screenshot 4 (b)).
Output:
Example #5 – Help about Sleep command
We have the functionality to get help with the Sleep command. To get any help, we need to use the “help” keyword before the Sleep command.
Command:
sleep --help
Explanation: As per the above command, we can get basic help with the sleep command. It will print general information like how the command is, what it does, etc.
Output:
Example #6 – Sleep Utility
We can check the sleep utility version in the Linux environment.
Command:
sleep --version
Explanation: As per the above version command, we can get the version information of the sleep command. Users use it to check the functionality of the sleep command. The recommendation is to go with the new or latest version mainly.
Output:
Conclusion
We have seen the uncut concept of “Linux Sleep Command” with the proper example, explanation, and command with different outputs. The sleep command is useful to pause the execution of the jobs or commands. We can use the sleep command combining both the Linux command and the shell jobs.
Recommended Article
We hope that this EDUCBA information on “Linux Sleep” was beneficial to you. You can view EDUCBA’s recommended articles for more information.