With the sleep command, your Linux computer can do nothing. Maybe it's not intuitive, but sometimes it's just a period of inactivity. This article shows you how to effectively use this bash shell command.
The use of
sleep is easy. On the command line
sleep enter a space, a number, and press Enter.
The cursor disappears for five seconds and then returns. What happened? If you use
sleep on the command line, Bash will instruct you to suspend processing for the duration you specified. In our example, that was five seconds.
We can output the duration in […]
d, h, m or
s with duration. To stop sleep for one day, four hours, seven minutes, and five seconds, enter a command as follows:
sleep 1d 4h 7m 5s
sleep will treat each duration as seconds. Suppose you wanted to sleep
for five minutes and twenty seconds . A correct format of this command is:
sleep 5m 20
If you forget to specify the suffix
m for the minutes, you will instruct
sleep to pause for five seconds and then again for twenty seconds.
sleep lasts for 25 seconds.
Many commands require you to specify the parameters in a specific order, but
sleep is very lenient. You can specify them in any order, and
sleep makes them meaningful. You can also specify a floating-point number as a parameter. For example, 0.5h is a valid way to indicate that you want
sleep to be paused for half an hour.
All following (increasingly eccentric) commands have
sleep for a break of 10 seconds.
sleep 5 5s
sleep 1 1 1s 1 1s 1 2
Pausing sleep before a command
sleep command can be used to pause before executing a command. This command will last 15 seconds and then beep.
sleep 15 && echo -en & # 39; 007 & # 39;
Using the sleep command between two commands
You can use
sleep to give a break between two commands. This command lists the files in your Documents directory, lasts for five seconds, and then changes the current working directory to your home directory:
ls -R ~ / Documents && sleep 5 && cd ~
Using Sleep to Suspend Execution of a Script
You can use the
sleep command in shell scripts to pause script execution for a specified amount of time. In general, you must do this so that a process takes sufficient time before the script continues to process. You can also use it to limit the requirements that a script places on another resource.
To demonstrate this, a script is used here that calls a Google […] web service with
curl . When you query the web service with the ISBN number of a book, it responds with a snapshot of JSON data for that book. We can analyze this data by passing it through the utility
jq to retrieve the title of the book. So that the script does not burden the web service, it stays inactive for a second between web requests.
Create a file with the following text and save it as
#! / bin / bash for a book in `cat $ 1` do echo $ book ":" curl -s https://www.googleapis.com/books/v1/volumes?q=isbn:$book | jq # items |  | .volumeInfo.title & # 39; Echo "" sleep 1 done echo "All done."
Enter the following command to set the execute permissions and make the script executable.
chmod + x check_book.sh
The script requires the
jq utilities. Use
apt-get to install these packages on your system if you are using Ubuntu or any other Debian-based distribution. Instead, use the package management tool of your Linux distribution on other Linux distributions.
sudo apt-get install curl
sudo apt-get install jq
Create a text file with the following numbers and save it as
9781565921276 9781874416685 9781565921672 9780521431088 9781491941591
Run the script
check_book.sh and pass in the file
books.txt as a parameter.
./ checkbook.sh books.txt
Requests are sent to the Google Web service every second. The title of the book appears shortly after each ISBN number.
That's all you can sleep
check_book.sh falls outside the scope of this article. The script was selected solely to illustrate a valid use of the
sleep command. If you want to know more about the two main parts of the script, read the project page
curl and the online manual