使用 sleep
命令。
例子:
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.
指定时间单位时也可以使用小数;例如sleep 1.5s
那么:
read -p "Press enter to continue"
PAUSE
等效的 bash
在 Python(问题最初标记为 Python)中,您需要导入时间模块
import time
time.sleep(1)
或者
from time import sleep
sleep(1)
对于 shell 脚本来说就是
sleep 1
它执行 sleep
命令。例如。 /bin/sleep
python -c "import time; time.sleep(1)"
而不是 sleep 1
将 python 答案与 Bash 结合起来:)
python -c "import time; time.sleep(0.8)"
。但接下来我们需要考虑 python 启动实际需要多长时间。您需要运行:date +%N; python -c "import time; time.sleep(0)"; date +%N
以确定 python 需要多少纳秒才能启动。但这也包括运行日期的一些开销。运行此 date +%N; date +%N
以找到该开销。 Python 在我的机器上的开销实际上接近 0.14 秒。所以我想要time.sleep(0.86)
。
运行多个睡眠和命令
sleep 5 && cd /var/www/html && git pull && sleep 3 && cd ..
这将在执行第一个脚本之前等待 5 秒,然后在再次更改目录之前再次休眠 3 秒。
&&
而不是 ;
?
&&
将等待第一个命令完成。
我意识到我对此有点晚了,但是您也可以调用 sleep 并通过不想要的时间。例如,如果我想等待 3 秒,我可以这样做:
/bin/sleep 3
4 秒看起来像这样:
/bin/sleep 4
在 Mac OSX 上,睡眠不需要几分钟等,只需几秒钟。所以两分钟,
sleep 120
在脚本中,您可以在您希望暂停的操作之间添加以下内容。这将使例程暂停 5 秒钟。
read -p "Pause Time 5 seconds" -t 5
read -p "Continuing in 5 Seconds...." -t 5
echo "Continuing ...."
-t 5
将在 5 秒后中止脚本,而不是继续,在 least according to this man page for read
read -r -p "Wait 5 seconds or press any key to continue immediately" -t 5 -n 1 -s
按任意一个按钮继续
有关详细信息,请查看 read
手册页 ref 1、ref 2
您可以使用默认随机数生成器 $RANDOM 让它等待。在下面我使用 240 秒。希望有帮助@
> WAIT_FOR_SECONDS=`/usr/bin/expr $RANDOM % 240` /bin/sleep
> $WAIT_FOR_SECONDS
在运行之前使用 trap
暂停并检查命令行(使用 tput
显示颜色)
trap 'tput setaf 1;tput bold;echo $BASH_COMMAND;read;tput init' DEBUG
按任意键继续
与 set -x 一起使用来调试命令行
thread.sleep
功能@Geremia