Software & Apps Linux 47 47 people found this article helpful How to Use the Linux Sleep Command to Pause a BASH Script Want to slow down your BASH script? Use the sleep command by Gary Newell Writer Gary Newell was a freelance contributor, application developer, and software tester with 20+ years in IT, working on Linux, UNIX, and Windows. our editorial process Gary Newell Updated on May 29, 2020 reviewed by Chris Selph Lifewire Tech Review Board Member Chris Selph is a CompTIA-certified technology and vocational IT teacher. He also serves as network & server administrator and performs computer maintenance and repair for numerous clients. our review board Article reviewed on Apr 16, 2020 Chris Selph Linux Switching from Windows Tweet Share Email The Linux sleep command, among other things, pauses a bash script. On its own, the sleep command isn't very useful. However, as part of a script, it can be used in many ways. For example, you can use it to pause the script before retrying a command that failed the first time. Kaley McKean / Lifewire An Example of Using the Sleep Command Imagine you have a script that processes files that were downloaded from another server. The script shouldn't start the copy process until all the files finish downloading. The download process is performed by a separate script that runs before yours. The script that copies the files may contain a loop to test whether all the files have downloaded (it does this by checking whether 50 files are found before starting the copy process). There's no point in the script testing constantly since this uses processor time. Instead, you might pause for a few minutes between each test before trying again. The sleep command is perfect in such circumstances. How to Use the Sleep Command To use the Linux sleep command, enter the following into the terminal window: The above command makes the terminal pause for 5 seconds before returning to the command line. The sleep command requires the keyword sleep, followed by the number you want to pause and the unit of measure. You can specify the delay in seconds, minutes, hours, or days. s: Secondsm: Minutesh: Hoursd: Days When it comes to pausing a script for days, use a cron job to run the script at regular intervals, as opposed to having a script run in the background for days. A cron job is a Linux command or script that you can schedule to run at a set time or day. These are useful for repeating tasks over a long period of time. The number for the sleep command interval doesn't have to be a whole number. You can also use floating-point numbers. For example, the following syntax includes a fraction of a second: An Example of Using the Sleep Command The following script shows how to use the sleep command to make a terminal-based countdown clock: #!/bin/bashx=10while [ $x -gt 0 ]dosleep 1sclearecho "$x seconds until blast off"x=$(( $x Here's how this script works: The script sets the variable x to 10.The while loop continues to iterate while the value of x is greater than zero.The sleep command pauses the script for 1 second each time around the loop.The rest of the script clears the screen each iteration, displays the message, "x seconds until blast off," and subtracts 1 from the value of x. Without the sleep command, the script would zoom through, and the messages would display too quickly. How to Use Sleep Command Switches The sleep command only has a couple of switches. The --help switch shows the help file for the sleep command. You can achieve the same thing by using the man command as follows: The --version switch shows the version of the sleep command that's installed on the system. The information returned by the --version switch is as follows: Version numberCopyright detailsLicenseAuthors Pause Terminal Commands with Sleep Another good use for the sleep command is to pause commands that you type in the terminal window. If you want, you can type two commands in a row, waiting for the first one to finish before typing the second. However, a faster approach is to type the two commands on one line, with a sleep command between each command: $ cd /mydirectory/ && slee How this command works: The cd /mydirectory/ command changes the directory.The sleep 3 command waits three seconds for the cd command to finish.The ls command executes and displays the directory contents. For a simple example like this, the sleep command only saves a little bit of time. However, if you have a long list of commands, the ability to type the commands on one line saves time.