Sleep Command in Linux

The suspend command is used to delay for a fixed period of time during the execution of any script. When the encoder needs to pause the execution of any command for the particular purpose, this command is used with the particular time value. You can set the amount of delay by seconds (s), minutes (m), hours (h), and days (d). This tutorial will help you learn how to use the suspend command by using different bash scripts.

Suspend command syntax:

suspend number[suffix]

You can use any integer or fractional number as the time value. The suffix part is optional for this command. If you omit the suffix, the time value is calculated as seconds by default. You can use s, m, h, and d as the suffix value. The following examples show the use of the sleep command with different suffixes.

Example-1: Suspend

command without any suffix In the

following script, the suspend command is used only with the numeric value 2 and no suffix is used. Therefore, if you run the script, the string “Task completed” will be printed after waiting 2 seconds.

Run the bash file with the time command to display the three types of time values to run the script. The output shows the time used by a system, user, and real time.

Output

:

Example-2: Suspend command with a minute suffix

In the following script, ‘m‘ is used as a suffix with the suspend command. Here, the time value is 0.05 minutes. After waiting 0.05 minutes, the message “Task completed” will be printed.

Run the script with the time command as in the first example.

Output

:

Example-3: Suspend command with time suffix In the following script, ‘h’ is used as a

suffix with suspend command

. Here, the time value is 0.003 hour. After waiting 0.003 hours, “Task completed” should be printed on the screen, but it actually requires more times when the ‘h’ suffix is used.

Output

:

Example-4: Loop Suspend Command

You can use the suspend command for several purposes. In the following example, the sleep command is used with while loop. Initially, the value of the variable n is set to 1 and the value of n will be increased by 1 for 4 times in each interval of 2 seconds. So when you will run the script, each output will appear after waiting 2 seconds.

Output

:

Example-5: Suspend command in terminal with other

commands

Suppose you want to run multiple commands and set the fixed time interval between the outputs of two commands, then you can use the suspend command to perform that task. In this example, the ls and pwd command are with the suspend command. After running the command, the ls command will display the directory list of the current directory and display the path of the current working directory after waiting 2 seconds.

Output

:

Example-6: The suspend command of the command prompt suspend command

is used between two echo commands in the following example. Three time values will be displayed after running the command.

Output:

sleep command is a useful command when you need to write a bash script with multiple commands or tasks, the output of any command may require a large amount of time, and another command must wait to complete the task of the previous command. For example, you want to download sequential files, and the next download cannot be started before completing the previous download. In this case, it is better to suspend the command before each download to wait for the fixed amount of time.