How to Pause a Bash Script | Linux Sleep Command

While creating a bash script in Linux, several commands are used one by one to perform a specific task. To pause the execution of a script or command for a specific time, you must specify the length of time you want to wait, followed by the “sleep” command.

The “sleep” command in Linux is a simple but powerful tool that allows the user to pause the commands or script execution for a specified time.

This guide will elaborate on the uses of the sleep command in the bash script with this timeline:

Let’s begin with the basic understanding of the sleep command.

What is Sleep Command in Linux?

The sleep command is used to sleep the system, which adds the delay between the two commands. The general syntax for the sleep command is as follows:

$ sleep Number[suffix]
  • sleep: Use to add delay in the system.
  • Number: Replace it with the desired number of seconds, minutes, or hours.
  • suffix: Replace it with a specified time. such as days(d),  hours (h), minutes (m), or seconds(s).

Here are examples of different sleep periods in Linux:

$ sleep 2 $ sleep 2sTo sleep the system for two (2) seconds. (By default seconds) 
$ sleep 3mTo add the delay for three (3) minutes.
$ sleep 2hh option is used for an hour; this command adds the sleep time for two hours.
$ sleep 1dTo sleep the system for one day.
$ sleep 2d 5h 30m 12sSleep command accept multiple suffixes. For instance, it delays the system for 2 days, 5 hours, 30 minutes, and 12 seconds.

How to Use Linux sleep Command to Pause a Bash Script?

The sleep command is very useful in the bash script when we want to complete the previous task and then execute the next command. We can pause the bash script with the sleep command; let’s use the sleep command in the bash script with the help of examples.

Example 1: Pause Bash Script for 10 seconds

The bash script runs several commands one by one. To add the sleep time of ten (10) seconds before executing an echo command, use the below command:

#!/bin/bash

Str1="Print after 10 seconds";
sleep 10
echo $Str1

To execute the bash script, use this command:

$ bash sleep.sh

It will delay the system for 10 seconds and then execute the echo command as shown in the above output.

Example 2: Set up an Alarm

We can sleep the system for a specified time and then runs the alarm tone to wake up. For instance, we want to take 7 hour’s break and then run the alarm.mp3 using the below command:

$ sleep 7h && vlc alarm.mp3

Example 3: Create a Digital Clock

To create a digital clock, the sleep command is used. The digital clock runs after every second and updates the time. The below while loop script can be used to provide a delay of every second and updates the time as in digital clock:

Let’s understand the code line-by-line:

#!/bin/bash

while true
do
clear
echo $(date +%T)
sleep 1s
done
  • while: Checks the loop if the condition is true.
  • Clear: Clears the screen every time and prints the time on a blank screen.
  • echo: Prints the date in time format (%T) only.
  • sleep: Delay the system for 1 second and show the time after every second.

To execute the script, the following command is used:

$ bash sleep.sh

Example 4: Pause Bash Script Using for loop

The sleep command can be used with the for loop to run the loop after a specific time delay. For instance, to print the numbers after every 3 seconds using the for loop, the below command is used:

#!/bin/bash

for i in {1..3}
do
  echo $i
  sleep 3s
done
echo "Task completed by $HOSTNAME"

To execute the above for loop bash script, use this command:

$ bash sleep.sh

That’s all from this post!

Conclusion

The sleep command adds the delay time in the bash script, which runs after specific time intervals such as seconds (s), minutes (m), hours (h), and days (d). This guide performs examples to create bash scripts that add 10 seconds delay to show output, 1 second delay to create a digital clock, and 3 seconds delay to print a number using the for-loop. This post has addressed the usage of the Linux sleep command to pause a bash script.