How to Sleep or Pause a Bash Script For a Specified Time

This post will guide you how to sleep or delay a bash script execution for a specified time under your Linux operating systems. How do I delay for a specified amount of time in seconds, minutes, hours or days using the Sleep command in a bash shell script on a Linux.

Pause a Bash Script For a Specified Time1

Sleep Command


Sleep Command can be used to Pause for NUMBER seconds. SUFFIX may be ‘s’ for seconds (the default), ‘m’ for minutes, ‘h’ for hours or ‘d’ for days. NUMBER need not be an integer. Given two or more arguments, pause for the amount of time specified by the sum of their values.

The syntax of the sleep command is as follows:

sleep NUMBER[SUFFIX]...
sleep OPTION

Pausing Bash Script for 10 seconds Using Sleep Command


If you want to sleep for 10 seconds in your bash script, and you can use the following command:

sleep 10

Here is an example:

#/bin/bash

echo "start to pause script for 10 seconds"
sleep 10
echo "continue to run script"

Outputs:

[root@localhost ~]# ./mysleep.sh
start to pause script for 10 seconds
continue to run script
[root@localhost ~]#

Pausing Bash Script for 10 Seconds Using Read Command


You can also use another command called “read” to pause command execution for 10 seconds, just using the following command in your bash script:

read "your text" -t 5

Here is an example:

#/bin/bash
read -p "start to pause bash for 5 seconds \n" -t 5
echo "continue to run this script"

outputs:

[root@localhost ~]# ./mysleep.sh
start to pause bash for 5 seconds
continue to run this script

Conclusion


You should know that how to use pause your bash shell script for a specified time amount using sleep command or read command on your CentOS or RHEL or Ubuntu Linux system.

You might also like:

Sidebar



back to top