Sleep
There are many ways to make a program/script pause for a few seconds. Here are some of my favourites.
Windows
There are two built-in sleep functions you can include in command scripts (.cmd, .bat and PowerShell):
pause
The pause command pauses until you press a key. It has no option to set the period of time. While pausing, it displays the message “Press any key to continue…” (or “Press Enter to continue…:” in PowerShell).
timeout /nobreak /t 20
This will sleep for 20 seconds, displaying a constantly refreshing message saying “Waiting for N seconds”. With the /nobreak option you have to use Ctrl-C to cancel the timer. If you omit the /nobreak then pressing any key will stop the timer.
The GNU utilities for Win32 include sleep.exe, which can be used like this:
sleep.exe 20
This will pause for 20 seconds with no on-screen message. Ctrl-C will interrupt. You can use timings like 20s, 20m and 20h to indicate seconds, minutes and hours.
Also in PowerShell you can use the following:
Start-Sleep -Seconds 20
This is much like sleep.exe
in that it displays nothing on screen. PowerShell also uses sleep
as an alias for Start-Sleep
.
Unix/Linux
The sleep tool (/bin/sleep) is available to every command shell in Unix/Linux. The syntax for a 20 second sleep is just this:
sleep 20
This assumes the period is 20s (seconds). It also understands minutes, hours and days, using suffixes m, h and d, though a sleep for several days would be quite unusual! You can also specify more complex periods, such as “sleep 6h 20m 15s” which sleeps for six hours, 20 minutes and 15 seconds.
Pausing until a keypress occurs is a little more complex. This bash one-liner usually works:
read -n1 -r -p "Press any key to continue..." keypress
The key pressed by the user will be available in variable $keypress. If you want something that times out after 20 seconds, use this:
read -t20 -n1 -r -p "Press any key to continue..." keypress
This hack using /usr/bin/timeout is horrible, but it works:
timeout 20s tail -f /dev/null
Scripting
Obviously there are as many ways to sleep within a program as there are programming languages. More, if you include the many feature libraries that accompany these languages. Some languages have built-in sleep functions, and some of these can be accessed directly from the command line or a command-level script. This means that if you know that a certain scripting language is present, regardless of operating system, you have access to a sleep function. Scripting languages generally do not have on-screen message side-effects when sleeping, so if you want a message then output one before you do the sleep.
My favourite scripting language is Perl, and here is how to sleep for 20 seconds from the command line:
perl -e "sleep 20"
If you want to use Perl to pause until the user presses Enter, this should work:
perl -e "<>"
Python is a little more involved. The following sleeps for 20 seconds and can only be interrupted by Ctrl-C:
python3 -c "import time; time.sleep(20)"
You can also try this in Ruby:
ruby -e 'sleep(20)'
Note that most scripting languages can also access the underlying operating system and/or shell so they could invoke the system’s sleep tool, but that means the script is not OS-independent so I won’t discuss those options any further here.
Categorised as: Coding, Operating Systems, Technology, Uncategorized