2

sleep command can be used to introduce a delay. e.g. sleep 5 will sleep for 5 seconds.

Is there any alternative/way/method that will display the pending seconds on the terminal?

output like

 resuming in 5 sec

the numeric values just countdowns on the same line of terminal.

mtk
  • 27,530
  • 35
  • 94
  • 130
  • https://unix.stackexchange.com/questions/92919/progressbar-in-bash-to-visualize-the-time-to-wait https://unix.stackexchange.com/questions/314512/how-to-determine-the-amount-of-time-left-in-a-sleep? – muru Jul 29 '20 at 09:04

3 Answers3

5

A hacky approach using pv:

$ yes | pv -SeL1 -s 5 > /dev/null
ETA 0:00:04

Or:

$ yes | pv -SL1 -F 'Resuming in %e' -s 5 > /dev/null
Resuming in ETA 0:00:04

pv is normally used to give a progress bar for the I/O of some data stream. Here we're using the constant stream of the yes command, but rate-limiting it to 1 byte per second (-L1), and Stopping after the output has reached a size of 5 bytes.

You can add more in there like the elapsed time, ETA as in clock time when it's expected to finish, or even a progress bar:

$ yes | pv -SpeL1 -s 5 > /dev/null
[===========>                                                  ] 20% ETA 0:00:04

Note that the duration won't be exactly 5 seconds (closer to 6 actually here with pv 1.6.6 in my tests).

Speaking of progress bar, you can also feed its output to zenity (graphical) or diaglog/whiptail for an even fancier display:

yes | pv -SnL1 -s 5 2>&1 > /dev/null | 
  zenity --progress --auto-close --no-cancel \
         --title 'Please wait...' --text 'Please wait...'

zenity

yes | pv -SnL1 -s 5 2>&1 > /dev/null |
  whiptail --gauge 'Please wait...' 5 20 0

whiptail

For a smoother progress, you can increase the rate to 10 bytes per second (and multiply the cut-off size by 10) while telling it to give you 10 updates per second:

yes | pv -SpeL10 -i0.1 -s 50 > /dev/null
yes | pv -SnL10 -i 0.1 -s 50 2>&1 > /dev/null |
  whiptail --gauge 'Please wait...' 6 20 0
  • On AppleM1, the first command is giving weird output. The ETA time is jumping around randomly, e.g. if initiated to sleep for 60 sec, the time shown 00:00:34 -> 00:00:45 -> 00:00:15 -> 00:00:10 . – mtk Oct 10 '22 at 09:48
1

Running sleep 5 times?

for ((i=5;i>0;i--)); do printf '%s %s\r' "$i" 'seconds pending'; sleep 1; done; echo

The precision is quite good, there's just the overhead of the loop.

rexkogitans
  • 1,329
1