How to Use the Linux sleep Command with Examples

Introduction

When the user issues a multiple command sequence in Linux, the commands execute immediately one after another or concurrently (e.g., the tee command). However, sometimes it is necessary to postpone the execution of commands and provide enough time for the system to produce the expected results.

In this tutorial, you will learn how to use the Linux sleep command to delay command execution in the terminal and shell scripts.

How to Use the Linux sleep Command with Examples

Prerequisites

  • A system running Linux
  • Access to the command line

What Does the Linux sleep Command Do?

The sleep command suspends the calling process of the next command for a specified amount of time. This property is useful when the following command’s execution depends on the successful completion of a previous command.

Linux sleep Command Syntax Explained

The syntax of the sleep command is simple:

sleep [number]
Using the sleep command to introduce a 5-second delay in the terminal

In the example above, after sleep 5 was executed, the second command prompt appeared with a 5-second delay.

By default, the system reads the number after sleep as the number of seconds. To specify other time units, use the following syntax:

sleep [number][unit]
Using the sleep command to introduce a longer delay in the terminal

The sleep command accepts floating-point numbers. It allows multiple values, which are all added together to calculate the duration of sleep.

Available units are:

  • s – seconds
  • m – minutes
  • h – hours
  • d – days

To stop sleep after it started and before the specified waiting period ends, press Ctrl + C.

To see help for the sleep command, type:

sleep --help 

For version details, type:

sleep --version

Linux sleep Command Examples

The following sections contain examples of using the sleep command in the terminal or shell scripts.

Note: The sleep command is designed to work in combination with other Linux commands. For a list of available Linux commands, download our free Linux Commands Cheat Sheet.

Set up an Alarm

Use sleep to tell the system to play an mp3 file after a certain amount of time. The example uses mplayer:

sleep 7h 30m && mplayer alarm.mp3

Delay Commands in Terminal

sleep is useful for enforcing a time between the execution of two commands. The following example makes echo commands execute in one-second intervals:

sleep 1 && echo "one" && sleep 1 && echo "two"
Introducing a one-second delay between command execution using the sleep command

Assign a Variable to the sleep Command

It is possible to assign a variable to the sleep command. Consider the following shell script:

#!/bin/bash
SLEEP_INTERVAL="30"
CURRENT_TIME=$(date +"%T")
echo "Time before sleep: ${CURRENT_TIME}"
echo "Sleeping for ${SLEEP_INTERVAL} seconds"
sleep ${SLEEP_INTERVAL}
CURRENT_TIME=$(date +"%T")
echo "Time after sleep: ${CURRENT_TIME}"

The script defines a variable called SLEEP_INTERVAL whose value is later used as an argument to the sleep command. The output of this example script shows that the execution lasted 30 seconds:

Output of a script that shows the current time at the beginning and end of the execution

Define Check Intervals

The following example illustrates the use of the sleep command in a script that checks whether a website is online. The script stops if it successfully pings a website, and sleep introduces a 10-second delay between unsuccessful pings.

#!/bin/bash
while :
    do
        if ping -c 1 www.google.com &> /dev/null
        then
        echo "Google is online"
        break
        fi
    sleep 10
done
A script that checks availability of a website outputs a message that the website is online

Allow Time for Operation Completion

You may be running a bash script that internally calls two other bash scripts – one that runs tests in the background and another that prints the results. Use sleep to prevent the second script from printing the wrong results if it executes before the completion of the first script:

while kill -0 $BACK_PID ; do
    echo "Waiting for the process to end"
    sleep 1
done

The kill -0 $BACK_PID command checks if the first script’s process is still running. If it is, it prints the message and sleeps for 1 second before checking again.

Predict Latency

Use sleep to allow latency of certain command executions. The script snippet below shows how sleep gives the CPU enough time to perform the calculation before the next iteration.

for (( i = 1 ; i <= 250 ; i++ )); 
    do  
    sleep 1
    qsub computation"${i}".pbs
done

Conclusion

After reading this tutorial, you should know how to use the Linux sleep command to pause the execution of the commands in a sequence.

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.

原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/tech/dev/223896.html

(0)
上一篇 2022年1月7日 03:33
下一篇 2022年1月7日 03:35

相关推荐

发表回复

登录后才能评论