The sleep is one of the commonly used commands in many shell scripts. The other commands which we frequently use in shell scripts are an echo, read, print commands etc. In this post, we will cover sleep command and TMOUT system variable which are helpful in delaying execution in

What is the use of sleep command in Linux?

The only purpose of sleep command is to block or delay the execution of a particular script for a defined amount of time. This is a useful mechanism where you have to wait for a specific operation to complete before the start of other activity in your shell scripts. For example, you just start a service in a shell script, and that may take some time to show some results for another command. In these cases, sleep command is used to delay of execution of next command. We can customize how many seconds or minutes or hours or days you can pause the subsequent command execution.

Sleep command syntax in Linux

sleep NUMBER[SUFFIX]...
sleep OPTION

Let us take one example to understand this. Observe below simple shell script where we execute it without sleep and with sleep command. The time taken for both the scripts can be calculated using time command.

#!/bin/bash
echo "Hello World."
echo "Goodbye"

Execute above script to see how much time it takes.

root@linuxnix:~/sh# bash lets_sleep.sh 
Hello World.
Goodbye.
root@linuxnix:~/sh# time bash lets_sleep.sh 
time bash lets_sleep.sh 
Hello World.
Goodbye.
real 0m0.003s
user 0m0.004s
sys 0m0.000s

From the above, it is evident the script took no more than 0.003 seconds of real time. Now execute the script by adding sleep command with one-second delay and see the difference.

#!/bin/bash 
echo "Hello World." 
sleep 1
echo "Goodbye"

The output after we delay the execution.

root@linuxnix:~/sh# time bash lets_sleep.sh 
Hello World.
Goodbye.
real 0m1.006s
user 0m0.000s
sys 0m0.000s

Now the real time is 1.006 seconds; this is because of delay we added into our shell script.

Sleep command supports below units

  1. s for seconds; this is a default one if you don’t specify any letter after the integer.
  2. m for minutes.
  3. h for hours.
  4. d for days.
  5. Even sub-second as well with the help of integer formate.

Sleep command examples

Example1: Delay execution of a command for half a second.

root@linuxnix:~# time (echo hi;sleep 0.5;echo bye)
hi
bye
real 0m0.503s
user 0m0.000s
sys 0m0.000s

The other options available are number and number+s for seconds.

sleep 10

sleep 20s

The above sleep commands delay next command execution by 10 and 20 seconds respectively.

Example 2: Delay execution of a command for one minute.

root@linuxnix:~# time (echo hi;sleep 1m;echo bye)
hi
bye
real 1m0.002s
user 0m0.000s
sys 0m0.000s

If you want we can use units s as well. By default whenever you give number to sleep command, it just

Example 3: Delay execution of a command by one hour or even for a day.

sleep 1h

sleep 1d

Example 4: Set a timeout for your read command so that it will not wait indefinitely. We can use system variable called TMOUT to set this timeout for your shell script.

root@linuxnix:~/sh# cat lets_sleep.sh
#!/bin/bash
TMOUT=5
read -p "Enter your name:"
if [ -z $REPLY ]
then
echo -en "\nTaking the default name as Linux\n"
else
echo -en "Welcome $REPLY\n"
fi
root@linuxnix:~/sh# time bash lets_sleep.sh
Enter your name:surendra
Welcome surendra
real 0m2.227s
user 0m0.000s
sys 0m0.000s
root@linuxnix:~/sh# time bash lets_sleep.sh
Enter your name:
Taking the default name as Linux
real 0m5.002s
user 0m0.000s
sys 0m0.000s

If you observe when we don’t enter anything the read command waits for 5 seconds and then continue with other executions.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.