SoFunction
Updated on 2025-03-09

Specific use of the shell sleep command

1. Background

When a user issues multiple command sequences in Linux, the commands are executed immediately one after another or simultaneously (for example, the tee command). However, sometimes it is necessary to postpone the execution of the command and provide the system with sufficient time to produce the expected results.

2. Introduction

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

3. Syntax

sleep [number]

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

sleep [number][unit]
sleep 1h 2m 0.5s

The sleep command accepts floating point numbers. It allows multiple values ​​to be added to calculate sleep.

Available units are:

  • s-seconds
  • m-minute
  • h-hour
  • d-day

To stop sleep after the start and before the specified waiting time ends, press Ctrl + C.

To view the help of the sleep command, type:

sleep --help 

For version details, type:

sleep --version

4. Difference from wait

bash wait commandIs a shell command that waits for the process running in the background to complete and returns to the exit status. Unlike the sleep command that waits for a specified time, the wait command waits for all or specific background tasks to complete.

5. Example

5.1 Setting up an alarm

Used to sleep tell the system to play mp3 files after a certain time.

sleep 7h 30m && mplayer alarm.mp3

5.2 Delay commands in the terminal

Sleep is useful for enforcing the time between two commands, executing at one second interval:

$ sleep 1 && echo "one" && sleep 1 && echo "two"
one
two

5.3 Variable assignment to sleep

Variables can be assigned to the sleep command.

#!/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 named SLEEP_INTERVAL, whose value is later used as an argument to the sleep command. The output of this sample script shows that execution lasted for 30 seconds:

$ ./time_script.sh
Time before sleep: 00:01:15
Sleeping for 30 seconds
Time after sleep: 00:01:45

5.4 Define the check interval

Check if the website is online, if a website is successfully pinged, the script will stop, introducing a 10-second delay between unsuccessful pings.

#!/bin/bash
while :
  do
    if ping -c 1  &> /dev/null
    then
   echo "Google is online"
   break
   fi
 sleep 10
done

5.5 Set aside time for operation completion

You may be running a bash script that calls two other bash scripts internally—one running the test in the background, and the other printing the result. If the second script is executed before the first script is completed, it is used to sleep to prevent the second script from printing errors:

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

The kill -0 $BACK_PID command checks whether the process of the first script is still running. If yes, it prints the message and hibernates for 1 second, then checks again.

5.6 Predicted delay

Used to sleep to allow the execution of certain commands.

for (( i = 1 ; i <= 250 ; i++ )); 
    do  
    sleep 1
    <do something>
done

refer to:

How to Use the Linux sleep Command with Examples

This is the article about the specific use of shell sleep commands. For more relevant shell sleep command content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!