How to Use Bash sleep Command with Example

In bash, the sleep command adds a delay or pause to the execution of script instructions for a specific period. The delays in shell scripting can be used for various purposes, such as managing too many requests on the server, flow control, debugging, and resource management. 

What is Bash sleep Command

The sleep command in bash is used to insert delays and pauses into the execution of the instructions to control the flow of the script. It is also a handy tool to manage throttling. Moreover, it is used to create timers, and timeouts for processes as well.

Bash sleep Command Syntax

In bash, the sleep command is used with a number and a suffix. The suffix is a unit in seconds, minutes, hours, or days. The syntax of using the sleep command is as follows: 

sleep [number][unit]

The [number] can be a whole number or a floating point number. 

Different units are listed below:

UnitsDescription
sSeconds (Default)
mMinutes
hHours
ddays

Note: The above units or suffixes may not work in many Unix and Unix-like operating systems, such as macOS. 

To add a delay of 3 seconds, the sleep command will be used as:

sleep 3

To sleep for 300 milliseconds.

sleep 0.3

Sleep for 1 minute and 10 seconds:

sleep 1m 10s

The above command is equivalent to sleep 70.

Sleep for 1 hour, 25 minutes, and 30 seconds.

sleep 1h 25m 30s

Sleep for 1 day.

sleep 1d

To learn more about the sleep command, run the following commands in the terminal:

sleep --helpman sleep

Bash sleep Command Examples

Several examples of using the sleep command in shell scripting are listed below:

1. Adding Delay in Bash

To add a basic delay to a bash script, place the sleep command with a specific number in between the instructions. An example is as follows:

#!/bin/bash

echo "Wait for 3 seconds..."
sleep 3
echo "Wait finished!"

2. Loop Countdown Timer

The sleep command can also be used to create a countdown timer in bash. 

#!/bin/bash

echo "Countdown starts!"
for i in {10..1}
do
	sleep 1
	echo -n "$i "
done
echo "Countdown finishes!"

3. Backing Up a Directory with a Delay

If you want to back up a directory after a certain period then the sleep command can be used.

#!/bin/bash

directory=/home/user/backup

while true
do
	echo "Backing up!"
	tar czf $directory/backup_$(date +'%Y%m%d_%H%M%S').tar.gz --absolute-names /home/user/dir
	sleep 30m
done

In the above script, the /home/user/dir directory is backed up to the /home/user/backup directory every 30 minutes.

4. Polling using the sleep Command

Polling is a technique in which the script continuously checks for a condition after regular intervals. The condition can be the status of a process or the existence of a file or directory.

#!/bin/bash

while true
do 
	if [ -f "file.txt" ]
	then
		echo "File found!"
	break
	else
		echo "Waiting for the file.."
		sleep 5
	fi
done

In the above example, during the first 15 seconds, there was no file in the current directory. Then a file is created and the script finds it. 

5. Throttling Requests 

Throttling is a technique used to control the system resources, network bandwidth, and memory. The sleep command can be used to create throttling to the requests sent to a specific server. 

For instance, to download 10 files by sending 10 requests to a server, use the sleep command to insert a delay after each request, instead of sending all requests simultaneously.

#!/bin/bash

for i in {1..10}
do
     curl -s https://www.example.com/download_$i
     sleep 5
done

In the above script, the request is sent after every 5 seconds.

6. Checking Server Status in Regular Intervals

To check whether a host is online or not, the sleep command can be employed.

The given script is continuously checking the server, on finding the server inactive there will be 5 seconds delay, and then the loop will go for the next iteration. 

#!/bin/bash

while true
do 
	if ping -c 1 [server_address] &> /dev/null
	then
		echo "Server is online"
	break
	else
		echo "Server is offline"
	fi
	sleep 5
done 

Replace the [server_address] in the code with the actual server address.

7. Creating Timeout for Long Processes

The sleep command can also be used to create a timeout for a long background process. 

#!/bin/bash

background_process()
{
sleep 100
}

#Sending the process to the background
background_process &

#Inserting a timeout of 10 seconds
sleep 10

#Checking process
if ps -p $! > /dev/null
then
	echo "Process is running.. Terminating"
	kill $!
else
	echo "Process completed within the time limit"
fi

In the above script, a function for a long process is created and then sent to call it in the background using &.

Then a timeout is set using the sleep command. The if condition checks whether the process is finished within the specified timeout or not. 

The process will run for 100 seconds, that’s why the script is terminating it after 10 seconds. However, if the process runs for less than 10 seconds the instruction in the else condition will be executed.

8. Taking Screenshots with a Delay

If you want to take a screenshot at regular intervals, you can use bash scripting. The sleep command will be used to add delay and the gnome-screenshot utility will take the screenshots.

#!/bin/bash

while true
do
     gnome-screenshot
     sleep 60
done
Image 33

The above script takes a screenshot of the entire screen after the delay of 60 seconds. 

Conclusion

In bash, the sleep command is primarily used to insert delays to pauses. Moreover, it is a useful tool to control the execution of the script. This guide provides the basic syntax and usage. Moreover, different examples are also covered to highlight the usage of the sleep command in various scenarios.