於 Linux Bash shell 常常會將一堆命令使用 | 接著,一長串的執行下去。
但是某些指令不能下太快,某些程式會需要啟動、初始化等等,一直 | 接下去,會有問題,需要 sleep 一下。
不過 Linux sleep 平常都使用 sleep 1 (1秒),要怎麼停 0.5、0.001 秒呢?
Linux sleep 如何暫停半秒、微秒
於 Linux 想要暫停半秒、微秒,sleep 本身就可以吃 0.x 的秒數,所以直接 sleep 0.5 或 sleep 0.001 即可。
man sleep 的說明如下:
- Pause for NUMBER seconds.
- SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days.
- Unlike most implementations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number.
- Given two or more arguments, pause for the amount of time specified by the sum of their values.
文件裡面有支援浮點數,所以就可以直接嘗試:
-
- $ time sleep 0.5 # 500 milliseconds (1/2 of a second)
real 0m0.501s user 0m0.000s sys 0m0.000s
- $ time sleep 0.001 # 1 millisecond (1/1000 of a second)
real 0m0.003s user 0m0.000s sys 0m0.000s
- $ time sleep 1.0 # 1 second (1000 milliseconds)
real 0m1.001s user 0m0.000s sys 0m0.000s
- $ time sleep 1
real 0m1.001s user 0m0.000s sys 0m0.000s
- $ time (for f in
seq 1 10
; do sleep 0.1; done) # 跑10次 0.1秒real 0m1.009s user 0m0.000s sys 0m0.000s
- $ time sleep 1m # 1 minute
real 1m0.002s user 0m0.004s sys 0m0.000s
- $ time sleep 0.5 # 500 milliseconds (1/2 of a second)