How to Make a Linux Script Pause Before Continuing
10 Answers 10
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
answered Feb 7, 2014 at 6:52
RydallCooperRydallCooper
17.6k 1 gold badge 10 silver badges 16 bronze badges
4
And what about:
read -p "Press enter to continue"
answered Jan 6, 2017 at 10:34
VontonVonton
2,598 3 gold badges 20 silver badges 25 bronze badges
5
-
Actually, this answered my question, even if it doesn't answer the OP.
Mar 18, 2017 at 1:56
-
this has nothing to do with this post.
Feb 27, 2018 at 7:47
-
@murtadhaalsabbagh, But has very much to do with Google indexing :) Helped me too, btw.
Jun 27, 2019 at 20:37
-
read -p "Press enter to continue" -t 1 should pause for 1 sec
Mar 4, 2020 at 15:11
-
@JesseChisholm I guess people are looking for the bash equivalent of the powershell/cmd
PAUSE
Mar 15, 2020 at 3:35
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
answered Feb 7, 2014 at 6:04
John La RooyJohn La Rooy
286k 51 gold badges 359 silver badges 499 bronze badges
4
-
yes, this says it too, it's just the other one was first, and it has a nice example :) but +1!
–user3268208
Feb 7, 2014 at 6:05
-
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 ofsleep 1
:)Jul 10, 2017 at 7:11
-
@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 thisdate +%N; date +%N
to find that overhead. Python's overhead on my machine was actually closer to 0.14 seconds. So I wanttime.sleep(0.86)
.Dec 12, 2017 at 21:51
-
True, though you'll never get exactly 1000ms, not even like that.
Dec 13, 2017 at 5:24
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.
answered Sep 19, 2018 at 12:17
Robot BoyRobot Boy
1,756 1 gold badge 16 silver badges 17 bronze badges
4
-
+1 ... cuz if you use one ampersand after the sleep it sends sleep off to its own thread then immediately starts the next action (i.e. the sleep doesn't delay the next action)
Sep 28, 2018 at 0:38
-
Why
&&
and not;
?Jun 10, 2021 at 17:17
-
@vrnvorona
&&
will wait for the first command to finish.Aug 6, 2021 at 7:11
-
@RobotBoy i think most importantly, if first command fails, && won't proceed, but ; will. ; will wait too, but won't check success.
Aug 7, 2021 at 8:03
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
answered Jul 30, 2015 at 4:48
pcmaster574pcmaster574
697 5 silver badges 4 bronze badges
1
-
I needed this not the python one. Thanks.
Sep 18 at 10:26
On Mac OSX, sleep does not take minutes/etc, only seconds. So for two minutes,
sleep 120
answered Aug 23, 2017 at 20:16
AnneTheAgileAnneTheAgile
9,648 6 gold badges 47 silver badges 48 bronze badges
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 ...."
answered Aug 16, 2017 at 18:37
3
-
@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
Nov 29, 2019 at 4:58
-
Hmmm.... I think you're right! I thought I tried this, but I just tried it again and it works as advertised. Thanks!
Nov 29, 2019 at 13:24
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
20k 9 gold badges 41 silver badges 61 bronze badges
answered Feb 25, 2018 at 5:29
Ole LukøjeOle Lukøje
502 4 silver badges 8 bronze badges
2
-
While this code snippet may be the solution, including an explanation 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.
Feb 25, 2018 at 5:33
-
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"
Aug 22, 2018 at 11:43
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
answered May 9, 2019 at 19:15
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
answered Nov 19, 2019 at 10:46
AkhilAkhil
814 11 silver badges 22 bronze badges
Source: https://stackoverflow.com/questions/21620406/how-do-i-pause-my-shell-script-for-a-second-before-continuing#:~:text=Use%20the%20sleep%20command.,5%20%23%20Waits%200.5%20second.
It seems on Mac OS X, the s, m, h, nor d have any impact. You must specify the time in seconds.
Aug 19, 2017 at 15:31
I think the naming is because we have
thread.sleep
function in many programming languages @GeremiaJan 31, 2018 at 13:14
More recently the suffix gives an error on Mac. "usage: sleep seconds"
Jul 1 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)
Sep 1 at 15:39