1085

I have only found how to wait for user input. However, I only want to pause so that my while true doesn't crash my computer.

I tried pause(1), but it says -bash: syntax error near unexpected token '1'. How can it be done?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

11 Answers11

1889

Use the sleep command.

Example:

sleep .5 # Waits 0.5 second.
sleep 5  # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.

One can also employ decimals when specifying a time unit; e.g. sleep 1.5s

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
RydallCooper
  • 19,094
  • 1
  • 11
  • 17
  • 92
    It seems on Mac OS X, the s, m, h, nor d have any impact. You must specify the time in seconds. – The AI Architect Aug 19 '17 at 15:31
  • 7
    I think the naming is because we have `thread.sleep` function in many programming languages @Geremia – Amin_mmz Jan 31 '18 at 13:14
  • More recently the suffix gives an error on Mac. "usage: sleep seconds" – nzifnab Jul 01 '22 at 18:33
  • If you get an "invalid time interval" you might want to check for your end of line setting, if you created your script with a Windows tool your end of line is "CR LF", you need to change it for Linux which is "LF" only. (Actually the error should be self explicit: invalid time interval '1\r' here you can see the extra \r from the \r\n) – Romano Sep 01 '22 at 15:39
200

And what about:

read -p "Press enter to continue"
Vonton
  • 2,872
  • 4
  • 20
  • 27
92

In Python (question was originally tagged Python) you need to import the time module

import time
time.sleep(1)

or

from time import sleep
sleep(1)

For shell script is is just

sleep 1

Which executes the sleep command. eg. /bin/sleep

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 6
    yes, this says it too, it's just the other one was first, and it has a nice example :) but +1! –  Feb 07 '14 at 06:05
  • 4
    So while not the right way to do it, you can combine the python answer with Bash by using `python -c "import time; time.sleep(1)"` instead of `sleep 1` :) – Berry M. Jul 10 '17 at 07:11
  • 3
    @BerryM. - Takes about 1.2 seconds when I try it. Python doesn't start up instantly - you need to account for that. Make it `python -c "import time; time.sleep(0.8)"` instead. But then we need to factor in how long python startup actually takes. You need to run this: `date +%N; python -c "import time; time.sleep(0)"; date +%N` to determine how many nanoseconds python is taking to start. But that also includes some overhead from running date. Run this `date +%N; date +%N` to find that overhead. Python's overhead on my machine was actually closer to 0.14 seconds. So I want `time.sleep(0.86)`. – ArtOfWarfare Dec 12 '17 at 21:51
  • True, though you'll never get _exactly_ 1000ms, not even like that. – Berry M. Dec 13 '17 at 05:24
68

Run multiple sleeps and commands

sleep 5 && cd /var/www/html && git pull && sleep 3 && cd ..

This will wait for 5 seconds before executing the first script, then will sleep again for 3 seconds before it changes directory again.

Robot Boy
  • 1,856
  • 1
  • 17
  • 17
45

I realize that I'm a bit late with this, but you can also call sleep and pass the disired time in. For example, If I wanted to wait for 3 seconds I can do:

/bin/sleep 3

4 seconds would look like this:

/bin/sleep 4
pcmaster574
  • 707
  • 5
  • 4
44

On Mac OSX, sleep does not take minutes/etc, only seconds. So for two minutes,

sleep 120
AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48
23

Within the script you can add the following in between the actions you would like the pause. This will pause the routine for 5 seconds.

read -p "Pause Time 5 seconds" -t 5
read -p "Continuing in 5 Seconds...." -t 5
echo "Continuing ...."
Jose H. Rosa
  • 514
  • 4
  • 8
  • This wont actually work, will it? I think that `-t 5` will abort the script after 5 seconds, not continue, at [least according to this man page for read](http://linuxcommand.org/lc3_man_pages/readh.html) – Brad Parks Nov 18 '19 at 16:48
  • @BradParks Yes it will work Just in case: root@Joses-iPad:~ # cat readtest.bash #!/bin/bash echo "Here" read -p "Pause Time 5 seconds" -t 5 read -p "Continuing in 5 Seconds...." -t 5 echo "Continuing ...." echo "There" root@Joses-iPad:~ # cat re./st.bash Here Pause Time 5 secondsContinuing in 5 Seconds....Continuing .... There – Jose H. Rosa Nov 29 '19 at 04:58
  • Hmmm.... I think you're right! I thought I tried this, but I just tried it again and it works as advertised. Thanks! – Brad Parks Nov 29 '19 at 13:24
14
read -r -p "Wait 5 seconds or press any key to continue immediately" -t 5 -n 1 -s

To continue when you press any one button

for more info check read manpage ref 1, ref 2

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Ole Lukøje
  • 562
  • 5
  • 8
  • 8
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – peacetype Feb 25 '18 at 05:33
  • 2
    This answer is a code-only comment. It does not attempt to explain the code given. I feel the poster misinterpreted the question as "How do I wait for user input while simultaneously having a timeout on that user input" – Zimano Aug 22 '18 at 11:43
3

You can make it wait using $RANDOM, a default random number generator. In the below I am using 240 seconds. Hope that helps @

> WAIT_FOR_SECONDS=`/usr/bin/expr $RANDOM % 240` /bin/sleep
> $WAIT_FOR_SECONDS
3

use trap to pause and check command line (in color using tput) before running it

trap 'tput setaf 1;tput bold;echo $BASH_COMMAND;read;tput init' DEBUG

press any key to continue

use with set -x to debug command line

Akhil
  • 912
  • 12
  • 23
0

I think you can use software flow control.

Ctrl+s Freeze the terminal
Ctrl+q Unfreeze

https://en.wikipedia.org/wiki/Software_flow_control

$ while :; do sleep 0.2; echo hello; done
hello
hello
hello
hello

Use Ctrl+s to pause and Ctrl+q to resume the infinite loop.

k_vishwanath
  • 1,326
  • 2
  • 20
  • 28