Editing
Sleep
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== BASH == === Approach1: sleep === Tested on {{Linux}} & {{Mac}}<ref>[https://bash.cyberciti.biz/guide/Perform_arithmetic_operations Perform arithmetic operations - Linux Shell Scripting Tutorial - A Beginner's handbook]</ref><ref>[https://linuxize.com/post/how-to-use-linux-sleep-command-to-pause-a-bash-script/ Linux Sleep Command (Pause a Bash Script) | Linuxize]</ref> <pre> # 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') </pre> Sleep couple [https://en.wikipedia.org/wiki/Microsecond microsecond]s<ref>[https://serverfault.com/questions/151109/how-do-i-get-the-current-unix-time-in-milliseconds-in-bash How do I get the current Unix time in milliseconds in Bash? - Server Fault]</ref>. Tested on {{Linux}} <pre> # print current unix timestamp e.g. 1598346362244 date +%s%N # sleep 1 millisecond = 0.001 seconds sleep 0.001s # print current unix timestamp date +%s%N </pre> Sleep couple microseconds. Tested on {{Mac}} <pre> # print current unix timestamp e.g. 1598346362244 php -r 'echo microtime(TRUE) . PHP_EOL;' # sleep 1 millisecond = 0.001 seconds sleep 0.001s # print current unix timestamp php -r 'echo microtime(TRUE) . PHP_EOL;' </pre> Sleep couple microseconds. Tested on {{Mac}} <pre> # print current unix timestamp e.g. 1598346362244 Sleep couple microseconds. Tested on {{Mac}} <pre> # print current unix timestamp e.g. 1598346362244 php -r 'echo microtime(TRUE) . PHP_EOL;' # sleep 1 millisecond = 0.001 seconds sleep 0.001s # print current unix timestamp php -r 'echo microtime(TRUE) . PHP_EOL;' </pre> Sleep couple milliseconds (1 seconds = 1000 milliseconds) e.g. 1671090194.136471987<ref>[https://apple.stackexchange.com/questions/135742/time-in-milliseconds-since-epoch-in-the-terminal macos - Time in milliseconds since epoch in the terminal - Ask Different]</ref> <pre> # print current unix timestamp in milliseconds e.g. 1671090194.136471987 perl -MTime::HiRes=time -e 'printf "%.9f\n", time' # sleep 1 millisecond = 0.001 seconds sleep 0.001s # print current unix timestamp perl -MTime::HiRes=time -e 'printf "%.9f\n", time' </pre> === Approach2: ping === Tested on {{Linux}} & {{Mac}} ping local address: {{kbd | key=<nowiki>-c <Count></nowiki>}} "Stop after sending (and receiving) count ECHO_RESPONSE packets."<ref>[https://linux.die.net/man/8/ping ping(8) - Linux man page]</ref> <pre> # 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') </pre> === Approach3: usleep === [https://linux.die.net/man/3/usleep usleep(3) - Linux man page] "suspend execution for microsecond intervals" (1 second = 1 000 000 microsecond). Tested on {{Linux}} <pre> # print current date, time & nanoseconds echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N') # sleep 0.2 ~ 1.2 seconds timeout=$(($RANDOM * 1000000 / 32768 + 200000)) echo $timeout usleep $timeout # print current date, time & nanoseconds echo $(date '+%Y-%m-%d %H:%M:%S')$(date '+.%N') </pre> === BASH example: download file and sleep random seconds between 1 ~ 7 seconds === The following script checks if a specific file exists in a defined path. If the file doesn't exist, it downloads the file from a provided URL and then pauses for a random duration between 1 and 7 seconds. If the file already exists, it simply prints a message stating that the file was found and the download was skipped. <pre> if [ ! -f /path/to/paper.pdf ] then curl "https://website/to/pdf" -L -o /path/to/paper.pdf sleep $((1 + RANDOM % 7)) else echo "File found. Download skipped." fi </pre> The same command can be written on one line: <pre> if [ ! -f /path/to/paper.pdf ]; then curl "https://website/to/pdf" -L -o /path/to/paper.pdf; sleep $((1 + RANDOM % 7)); else echo "File found. Download skipped."; fi </pre> References * [https://www.cyberciti.biz/faq/bash-check-if-file-does-not-exist-linux-unix/ How to check if file does not exist in Bash - nixCraft]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information