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?
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
thread.sleep function in many programming languages @Geremiaenable sleep then builtin sleep [time]For those looking for the Bash equivalent of Windows Powershell/CMD's pause command.
In Bash use read with option -p specifying a prompt like:
read -p "Press enter to continue"
PAUSEIn 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
python -c "import time; time.sleep(1)" instead of sleep 1 :)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).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.
&& and not ;?&& will wait for the first command to finish.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
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 ...."
-t 5 will abort the script after 5 seconds, not continue, at least according to this man page for readread -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
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.
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
$WAIT_FOR_SECONDS really supposed to be on the next line? It seems like it was intended to be the argument to the /bin/sleep command. Why not use /bin/sleep $((RANDOM % 240))?