Sleep Command in Linux

FREE Online Courses: Click, Learn, Succeed, Start Now!

In this article, you will learn what Linux SLEEP command is, a brief history, the basic functioning of the sleep command, and the syntax, of the command. In the end, we will also be looking into some cool commands of the sleep command in the Linux terminal.

What is sleep command in Linux?

Understanding the sleep command may be slightly confusing, but once you get the hang of it, you will never forget or unlearn it. Sleep is a command-line-based utility in Linux-based operating systems that helps in suspending program execution for a specified amount of time.

The sleep command creates a dummy job that helps in delaying the execution of a command. In layman’s language, the sleep command tells the terminal to wait for the specified amount of time.

There might come some scenarios where you need to delay the execution of a script, that is exactly where the sleep command comes into the picture. You can delay the execution by specifying the amount of time. The following is the notation used to specify different units of time:

1. Seconds – s

2. Minutes – m

3. Hours – h

4. Days – d

History of Linux sleep command

The first appearance of the sleep command was in the 4th version of Unix. From the 2nd issue of the X/Open potability Guide of 1987, the sleep utility for Unix-like systems was a part of it. The tool was later inherited into the first version of POSIX and the Single Unix Specification.

The sleep command that comes as a part of the GNU Coreutils package was written, designed, and developed by Jim Meyering and Paul Eggert. Apart from Linux-based operating systems, the sleep command is also available in OS-9 shell, KolibriOS, Microsoft Windows, IBM i OS, and more.

In the KolibriOS shell, the sleep command is available as part of the FreeDOS package group Utilities. This version was developed by Trane Francks and is licensed under the GNU GPL. In the Windows operating system, the sleep command comes as a part of the UnxUtils collection of the native Win32 ports package.

Syntax of Linux sleep command

The syntax of the sleep command is very easy to understand:

sleep <number> <suffix>

Let us take a closer look at the fields in the syntax of the sleep command:

1. <number>

This field takes in the value of the time you want the terminal to wait.

2. <suffix>

The number you give in the <number> field needs to have a unit right? This field takes in exactly that, you specify the unit of time. If you don’t enter any suffix, it takes the default unit as “seconds”. You can specify ‘s’ for seconds,

‘M’ for minutes, ‘h’ for hours, and ‘d’ for days.

Now that we have laid down the basic fundamentals let us look at the sleep command in action.

Setting an alarm

We can use the sleep command as an alarm by delaying the execution of playing an audio clip by specifying the time. We can use any of the following 2 commands to do the same:

setting an alarm using the sleep command

another method to set an alarm using the sleep command

In the above commands, I have set an alarm (i.e playing the “alarm.mp3 file”) after eight hours and forty minutes (7h 30m)

Delaying commands in the terminal

The best use of sleep is to delay the execution of commands in the terminal. For example, If I want to delay the command echo “HELLO” by 5 seconds, I simply pair it with the “sleep 5” command using “&&” as shown:

delaying commands in the terminal

We can also combine 2 or more sleep commands. For example, the command shown below prints the text “HELLO” after a 5-second delay, followed by the text “BYE” after a 2-second delay after “HELLO” is printed.

delaying multiple commands in the terminal

Assigning a variable to the sleep command

The sleep command can also be stored in variables, let us explore how. Let us open a file and write a simple shell program as shown below:

shell program to assign a variable to the sleep command

output of the shell program assigning a variable to the sleep command

The shell script defines a variable called “SLEEP_INTERVAL” whose value was later used to define and print out the time after sleep.

Defining check intervals

The example below shows a beautiful example of the sleep command. In the following shell script, the code is written to check if google is online or not. The loop ends if it successfully pings google.com, but gives a 10-second delay between unsuccessful pings.

shell program to define check intervals

output of the shell program defining check intervals

Allowing time for completion of an operation

Say that you are running a shell script that internally calls 2 other shell programs, one that tests in the background and the other that prints the results. We can use the sleep command in the second script to avoid printing the wrong results if it executes before the first script executes.

shell program to allow time for completion of an operation

In the above shell script shown, the command “kill -0 $BACK_PID” cehcks if the first script is still running or not. If it is, The second program (the one shown above) prints the message and sleeps for 1 second before checking again.

Predicting the latency of a command

Make use of the sleep command to allow latency of execution of certain commands.

shell program to predict the latency of a command

The shell program shown above is an example of how the sleep command gives the CPU enough time to perform the calculations before the next iteration.

Using the sleep command with no suffix

We already discussed that if we don’t specify any suffix after the number, the sleep command takes the number to be in seconds.

using the sleep command with no suffix

Specifying a suffix after the number

We can specify any suffix out of ‘s’ for seconds, ‘M’ for minutes, ‘h’ for hours, and ‘d’ for days after the number in the sleep command. Here are some examples:

shell program that prints a text after few seconds

output of the shell program that prints a text after few seconds

We can even depict 2 seconds using the m suffix as “0.02m” as shown:

shell program that prints a text after few minutes

output of the shell program that prints a text after few minutes

We can also depict 2 seconds as “0.002” using the hour suffix as shown:

shell program that prints a text after few hours

output of the shell program that prints a text after some hours

 

Example of sleep command in a loop

We have already seen examples of the time command being used in shell programs, here is another example where the program sleeps for 3 seconds before going to the next iteration:

shell program using the sleep command in a loop

output of the shell program using the sleep command in a loop

Combining the sleep command with other Linux commands

We can combine the sleep command with most Linux commands by simply using the && operator as shown below:

combining the sleep command with other Linux commands

The command in the output shown above listed the contents of my current directory slept for 3 seconds and printed my present working directory.

Exiting from sleep mode

Say by mistake you have specified “10d” instead of “10s” or you just want to exit from the sleep time. To do so, you can press the key combination “ctrl” + “c”.

exiting from sleep mode

In the above output, i have given the delay to print as 2seconds to print “HELLO”, but 2 days to print “BYE”, so to excite the delay, I pressed “ctrl” + “c”

Pausing bash shell script for 10 seconds

To pause your bash shell for a couple of seconds (in this case 10 seconds), following the code shown below:

shell program to pause bash shell script for few seconds

Displaying help menu

To display the help menu just type the command “sleep –help”.

help menu of the sleep command

Printing version of sleep

To print information about the sleep utility you are y=using in your system, type the command “sleep –version” in the terminal.

version of the sleep command

Summary

As I said, the definition of the sleep command may look slightly intimidating at first, but now you have seen how easy it is, so easy that you can’t unlearn it! You have now learned what the sleep command is, a brief history of it, syntax, and wonderful examples of the sleep command in both the terminal and shell programs.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *