I need to use sleep
in a shell script, so I tried it out in a terminal, but the delays it produces are inconsistent and very inaccurate. For example sleep 3
produces a delay close to 20 seconds. These delays can also fluctuate when the same time is specified. In general the delay seems to increase exponentially with higher values.
Tried both on a Ubuntu and a Debian VM with similarly poor results. I do not think the VM component is in play (running a timeout 10
on a Windows VM is fine).
By timing each command the system clock thinks it's running okay but in reality it's not. See a few examples below.
Time in brackets is the actual time elapsed (approximate):
$ time sleep 1 (7 secs)
real 0m1.040s
user 0m0.003s
sys 0m0.016s
$ time sleep 1 (5 secs)
real 0m1.028s
user 0m0.009s
sys 0m0.013s
$ time sleep 1 (5 secs)
real 0m1.027s
user 0m0.013s
sys 0m0.007s
$ time sleep 1 (5 secs)
real 0m1.029s
user 0m0.007s
sys 0m0.016s
$ time sleep 3 (17 secs)
real 0m3.036s
user 0m0.000s
sys 0m0.021s
$ time sleep 5 (29.5 secs)
real 0m5.026s
user 0m0.007s
sys 0m0.013s
The default is obviously in seconds but adding an s
to the time doesn't make any difference.
Nothing else is running on the host machine that might hog disk or CPU.
Rebooting the VM seems to improve the situation for the first few tries but, after that, accuracy gets increasingly worse.
Any idea as to what the issue might be?
EDIT:
running
declare -p PS1
returnsdeclare -- PS1="\${debian_chroot:+(\$debian_chroot)}\\u@\\h:\\w\\\$ "
running
command -V sleep
returnssleep is hashed (/usr/bin/sleep)
running
declare -p PATH
returnsdeclare -x PATH="/home/debwp/mycmds:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
Results from Paul_Pedant's post:
~$ date '+%T.%N'; time sleep 5; date '+%T.%N' 22:47:49.679497552 ^[[A ^[[A ^[[A ^[[A real 0m5.033s user 0m0.005s sys 0m0.014s 22:47:54.788302324 ~$ date '+%T.%N'; time sleep 5; date '+%T.%N' 22:47:54.830674809 real 0m5.043s user 0m0.008s sys 0m0.012s 22:47:59.934542825 ~$ date '+%T.%N'; time sleep 5; date '+%T.%N' 22:47:59.994006022 real 0m5.057s user 0m0.004s sys 0m0.018s 22:48:05.159303996 ~$ date '+%T.%N'; time sleep 5; date '+%T.%N' 22:48:05.241043114 real 0m5.099s user 0m0.004s sys 0m0.021s 22:48:10.383158635 ~$ date '+%T.%N'; time sleep 5; date '+%T.%N' 22:48:10.435520982 real 0m5.028s user 0m0.004s sys 0m0.012s 22:48:15.497877219 ~$
entering
date
at the terminal at the rate of ~ once per second results in$ date Mon 31 Aug 20:42:25 CEST 2020 $ date Mon 31 Aug 20:42:25 CEST 2020 $ date Mon 31 Aug 20:42:25 CEST 2020 $ date Mon 31 Aug 20:42:26 CEST 2020
time
is accurately reporting), and your prompt refresh. Can you rundeclare -p PS1
and edit the output into your question? Alsocommand -V sleep
in case it has been aliased or you have a duplicate. Anddeclare -p PATH
in case that is searching a huge directory.date
x 2 around thetime sleep 5
omly confirms the output of the time command. The format'+%T.%N'
shows nanoseconds (first 3 digits only should be accurate). I want to show the delay in bash between the end of executing one command line and the execution of the next: I think the lost time is in bash reading the next command, not the sleep. So multiple date commands should be entered even before the sleep ends.