The sleep command won’t let your Linux computer do anything. Counterintuitive maybe, but sometimes a period of inactivity is just the thing. This article will teach you how to use this bash shell command effectively.
Using sleep
is simple. Enter on the command line sleep
, a space, a number, and then press Enter.
sleep 5
The cursor disappears for five seconds and then returns. What happened? Using sleep
on the command line, instructs Bash to suspend processing for the length of time you specify. In our example that was five seconds.
We can last surrender to sleep
in days, hours and minutes as well as in seconds. To do this, add a suffix to either d, h, m,
or s
with duration. To pause sleep for a day, four hours, seven minutes, and five seconds, use a command like this:
sleep 1d 4h 7m 5s
the s
Suffix (for seconds) is optional. Without a suffix, sleep
treats each duration as seconds. Suppose you wanted to have sleep
Pause for five minutes and twenty seconds. A correct format for this command is:
sleep 5m 20
If you forget to indicate that m
Suffix for the minute, you will instruct sleep
pause for five seconds and then again for twenty seconds. So sleep
will pause for 25 seconds.
Many commands require you to specify parameters in a specific order, but sleep
is very forgiving. You can deploy them in any order and sleep
will make sense of them. You can also specify a floating point number as a parameter. To the example, 0.5h is a valid option to indicate your request sleep
pause for half an hour.
Tell all of the following (increasingly eccentric) commands sleep
Pause for 10 seconds.
sleep 10
sleep 5 5s
Sleep 1 1 1s 1 1 1s 1 2
sleep 0.16667m
Use hibernation to pause before a command
the sleep
Command can be used to pause before executing a command. This command would pause for 15 seconds and then give a beep.
sleep 15 && echo -en '07'
Use sleep mode to pause between commands
You can use sleep
to put a pause between two commands. This command would list the files in your Documents directory, pause for five seconds, and then change the current working directory to your home directory:
ls -R ~/Documents && sleep 5 && cd ~
Use sleep mode to pause a script from running
You can use the … sleep
Command in shell scripts to pause script execution for a specified time. Typically, you would do this to give a process enough time to complete before the script continues processing. You can also use it to limit the rate at which a script requests another resource.
To demonstrate exactly that, here is a script that can be run using. calls a Google web service curl
. If you use the web service with the ISBN Number of a book, it replies with a dump of the JSON data for this book. We can analyze this data by going through the jq
Utility to get the title of the book. So that the script doesn’t load the web service, it sleeps for a second between web requests.
Create a file with the following text and save it as check_book.sh
.
#!/bin/bash for book in `cat $1` do echo $book":" curl -s https://www.googleapis.com/books/v1/volumes?q=isbn:$book | jq '.items | .[] | .volumeInfo.title' echo "" sleep 1 done echo "All done."
Enter the following command to set execute permissions and make the script executable.
chmod +x check_book.sh
The script requires the curl
and jq
Utilities. Use apt-get
to install these packages on your system if you are using Ubuntu or any other Debian based distribution. For other Linux distributions, use your Linux distribution’s package management tool instead.
sudo apt-get install curl
sudo apt-get install jq
Create a text file with the following numbers and save it as books.txt
.
9781565921276 9781874416685 9781565921672 9780521431088 9781491941591
Run the check_book.sh
Script and pass that books.txt
File as a parameter.
./check_book.sh books.txt
The inquiries are sent to the Google web service every second. The title of the book appears shortly after each ISBN number is queried.
That’s all there is sleep
. The inner workings of the check_book.sh
Scripting is beyond the scope of this article. The script was chosen solely to ensure valid use of the sleep
Command. If you want to learn more about the two main components of the script, check out the curl
Project page and the jq
Online manual .
Linux commands | ||
Files | tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm | |
Processes | alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg | |
Networking | netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw |
Best Linux Laptops for Developers and Enthusiasts