Sleep: Difference between revisions
Jump to navigation
Jump to search
| Line 28: | Line 28: | ||
Verify the installation of {{kbd | key=<nowiki>sleep</nowiki>}} package<ref>[https://stackoverflow.com/questions/9260014/how-do-i-install-cygwin-components-from-the-command-line How do I install cygwin components from the command line? - Stack Overflow]</ref><ref>[http://polarhome.com/service/man/?qf=SLEEP&af=0&tf=2&of=Cygwin SLEEP man page on Cygwin]</ref> | Verify the installation of {{kbd | key=<nowiki>sleep</nowiki>}} package<ref>[https://stackoverflow.com/questions/9260014/how-do-i-install-cygwin-components-from-the-command-line How do I install cygwin components from the command line? - Stack Overflow]</ref><ref>[http://polarhome.com/service/man/?qf=SLEEP&af=0&tf=2&of=Cygwin SLEEP man page on Cygwin]</ref> | ||
# Key-in {{kbd | key=<nowiki>cmd</nowiki>}} to open command prompt ([https://www.lifewire.com/how-to-open-command-prompt-2618089 How to Open Command Prompt (Windows 10, 8, 7, Vista, XP)]) | # Key-in {{kbd | key=<nowiki>cmd</nowiki>}} to open command prompt ([https://www.lifewire.com/how-to-open-command-prompt-2618089 How to Open Command Prompt (Windows 10, 8, 7, Vista, XP)]) | ||
# Key-in one of following | # Key-in one of following commands | ||
#* Key-in {{kbd | key=<nowiki>where sleep</nowiki>}} if you add installation path of CygWin to system path ([[How to setup my system path]]). Or | #* Key-in {{kbd | key=<nowiki>where sleep</nowiki>}} if you add installation path of CygWin to system path ([[How to setup my system path]]). Or | ||
#* Key-in {{kbd | key=<nowiki>dir C:\cygwin64\bin\sleep.exe</nowiki>}} if the installation path of CygWin is {{kbd | key=<nowiki>C:\cygwin64\</nowiki>}}. | #* Key-in {{kbd | key=<nowiki>dir C:\cygwin64\bin\sleep.exe</nowiki>}} if the installation path of CygWin is {{kbd | key=<nowiki>C:\cygwin64\</nowiki>}}. | ||
Revision as of 15:57, 5 November 2018
Sleep random seconds in programming
MS-DOS
Approach 1: %RANDOM% + ping local
batch file - .bat random timeout - Stack Overflow
- %RANDOM% returns an integer between 0 and 32767.
- 60 is the range of values you want: 0 to 60.
- 32768 is the range of values returned by %RANDOM% (0 to 32767).
- 5 is the minimum value you want. The original range of values you want '0 to 60' became '5 ~ 65'.
- ping local address: /n <Count> "Specifies the number of echo Request messages sent. The default is 4."[1][2]
BAT file:
REM print current date & time ECHO %date% %time% REM sleep 5 ~ 65 seconds SET /a timeout=%RANDOM% * 60 / 32768 + 5 ping 127.0.0.1 -n %timeout% > nul REM print current date & time ECHO %date% %time%
Approach 2: sleep package of CygWin
Requirement: Install Cygwin on Win
& install sleep package (How-to: Setting Up Cygwin/X)
Verify the installation of sleep package[3][4]
- Key-in cmd to open command prompt (How to Open Command Prompt (Windows 10, 8, 7, Vista, XP))
- Key-in one of following commands
- Key-in where sleep if you add installation path of CygWin to system path (How to setup my system path). Or
- Key-in dir C:\cygwin64\bin\sleep.exe if the installation path of CygWin is C:\cygwin64\.
C:\Users\user>dir C:\cygwin64\bin\sleep.exe # successful condition C:\cygwin64\bin\sleep.exe # failed condition File Not Found
BAT file:
REM print current date & time ECHO %date% %time% REM sleep 5 ~ 65 seconds SET /a timeout=%RANDOM% * 60 / 32768 + 5 C:\cygwin64\bin\sleep.exe %timeout% REM print current date & time ECHO %date% %time%
Approach 3: TIMEOUT
Alternative command:
The following command ECHO %date% %time% will not be executed after TIMEOUT %timeout% was executed.
BAT file:
SET /a timeout=%RANDOM% * 60 / 32768 + 5 TIMEOUT %timeout% ECHO %date% %time%
BASH
Approach1: sleep
Tested on Linux
& macOS
[5]
# print current date & time echo $(date '+%Y-%m-%d %H:%M:%S') # sleep 5 ~ 65 seconds timeout=$(($RANDOM * 60 / 32768 + 5)) sleep $timeout # print current date & time echo $(date '+%Y-%m-%d %H:%M:%S')
Approach2: ping
ping local address: -c <Count> "Stop after sending (and receiving) count ECHO_RESPONSE packets."[6]
# print current date & time echo $(date '+%Y-%m-%d %H:%M:%S') # sleep 5 ~ 65 seconds timeout=$(($RANDOM * 60 / 32768 + 5)) ping 127.0.0.1 -c $timeout > nul # print current date & time echo $(date '+%Y-%m-%d %H:%M:%S')
Approach3: usleep
usleep(3) - Linux man page "suspend execution for microsecond intervals" (1 second = 1 000 000 microsecond). Tested on Linux
# print current date & time echo $(date '+%Y-%m-%d %H:%M:%S') # sleep 0.2 ~ 1.2 seconds timeout=$(($RANDOM * 1000000 / 32768 + 200000)) echo $timeout usleep $timeout # print current date & time echo $(date '+%Y-%m-%d %H:%M:%S')