Linux / UNIX: Bash Script Sleep or Delay a Specified Amount of Time

How do I pause for 5 seconds or 2 minutes in my bash shell script on a Linux or Unix-like systems?

You need to use the sleep command to add delay for a specified amount of time. The syntax is as follows for gnu/bash sleep command:
sleep NUMBER[SUFFIX][donotprint]
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements bash
Time 1m
[/donotprint] Where SUFFIX may be:

ADVERTISEMENTS

  1. s for seconds (the default)
  2. m for minutes.
  3. h for hours.
  4. d for days.

Please note that the sleep command in BSD family of operating systems (such as FreeBSD) or macOS/mac OS X does NOT take any suffix arguments (m/h/d). It only takes arguments in seconds. So syntax for sleep command for Unix like system is:
sleep NUMBER

Examples

To sleep for 5 seconds, use:
sleep 5
Want to sleep for 2 minutes, use:
sleep 2m
Halt or sleep for 3 hours, use:
sleep 3h
More examples:

### To sleep for 5 days, use: ##
sleep 5d
 
#####################################################################
# BSD/Unix/macOS implementations of sleep command have required that number 
# be an integer, and only accepted a single argument without a suffix. 
# However, GNU/Linux version of sleep command accepts arbitrary floating 
# point numbers.
#####################################################################
 
### To sleep for 1.5 seconds: ##
sleep 1.5
 
## To sleep for .5 seconds: ###
sleep .5

The most common usage are as follows:

## run commmand1, sleep for 1 minute and finally run command2 ## 
command1 && sleep 1m && command2
 
## sleep in bash for loop ##
for i in {1..10}
do
  do_something_here
  sleep 5s
done
 
## run while loop to display date and hostname on screen ##
while [ : ]
do
    clear
    tput cup 5 5
    date
    tput cup 6 5
    echo "Hostname : $(hostname)"
    sleep 1
done

Sample outputs from last while loop:

Animated gif.01: Sleep command in action

Animated gif.01: Sleep command in action

sleep Command Bash Script Example

Here is a simple example:

#!/bin/bash
echo "Hi, I'm sleeping for 5 seconds..."
sleep 5  
echo "all Done."

In this example, create the lock directory. I’m using [/\\:.-] here to ensure that we don’t use the same name that we are using for the .o file. Also, base the name on the expected object file name, since that is what matters with a parallel build.

...
...
lockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
while true; do
  if mkdir "$lockdir" >/dev/null 2>&1; then
    break
  fi
  sleep 1
done
....
..
....

How can I pause my bash shell script for 5 second before continuing?

Use the read command:
read -p "text" -t 5
read -p "Waiting five secs for Cloudflare to clear cache...." -t 5
echo "Generating pdf file now ...."

Sample outputs:
Waiting five secs for Cloudflare to clear cache....
Generating pdf file now ....

Where,

  • -p "text" : Show the text without a trailing newline before time out.
  • -t N : Set time out to 5 seconds.

For more info see bash command man page here and here or read it by typing the following man command:
$ man bash
$ man sleep
$ help read

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
10 comments… add one
  • droopy4u Feb 13, 2011 @ 14:41

    with the command ‘sleep’ , for more information use the manual pages of sleep by ‘man sleep’.

    sleep 5s ; echo ‘end test sleep 5sec’
    sleep 1m; echo ‘end test sleep 1min’

    best regards

  • Nicholas Feb 14, 2011 @ 1:17

    Thanks, nice tip!

  • Terry McCall Aug 20, 2013 @ 17:39

    FYI for searchers who are OSX users:

    The sleep command in OSX does NOT take any suffix arguments (m/h/d). It only takes arguments in seconds.

    • ck Jan 8, 2015 @ 18:58

      FYI for searchers who are Android users:
      Drop the suffix, it goes by seconds:
      Sleep 3

  • Serg Dec 27, 2015 @ 8:54

    There is two types of sleep command – a shell built in and the /bin/seep. Both bash built-in and /bin/sleep take the suffixes, however the built in for mksh (which is on android) does not take suffixes. It’s is the same distinction between /usr/bin/time and shell built-in time

  • robert Jan 30, 2016 @ 8:56

    You have an excellect display of quick and simple scripts.
    Thank you,
    Bob

  • Bruno Vernay Sep 11, 2016 @ 18:39

    watch also would be handy:
    watch runs command repeatedly, displaying its output and errors (the first screenfull). This allows you to watch the program output change over time. By default, the program is run every 2 seconds. By default, watch will run until interrupted.

  • remco Sep 17, 2016 @ 11:11

    Hi,

    How i can make a script for my centos7 server dat shutdown my server on 0:00 and boot it at 12:00 ?

    Thanks

    • test Oct 11, 2016 @ 11:08

      you can’t

  • mytechskill Apr 5, 2017 @ 8:09

    Hi,

    I have a scenario that files for a day comes in the interval of 5-6 hour which is not exactly fixed some times may vary much and I have to keep eyes on them before executing my process.
    Can we schedule my process in such a way that it invokes multiple times if it fails in its previous attempt. Once it executed won’t be invoked again for the day.

    Thanks in Advance…!!!

    Regards,
    mytechskill

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.