bash脚本 sleep_如何使用Linux Sleep命令暂停Bash脚本

bash脚本 sleep_如何使用Linux Sleep命令暂停Bash脚本_第1张图片

bash脚本 sleep

Bash shell on Unity desktop concept Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

The sleep command makes your Linux computer do nothing. Counter-intuitive perhaps, but a period of inactivity is sometimes just what’s needed. This article shows you how to use this Bash shell command effectively.

sleep命令使您的Linux计算机不执行任何操作。 也许违反直觉,但有时只是需要一段时间不活动。 本文向您展示如何有效使用此Bash shell命令。

Using sleep is easy. On the command line type sleep, a space, a number, and then press Enter.

使用sleep很容易。 在命令行上,输入sleep ,一个空格,一个数字,然后按Enter。

sleep 5

The cursor will disappear for five seconds and then return. What happened? Using sleep on the command line instructs Bash to suspend processing for the duration you provided. In our example this was five seconds.

光标将消失五秒钟,然后返回。 发生了什么? 在命令行上使用sleep指示Bash在您提供的持续时间内暂停处理。 在我们的示例中,这是五秒钟。

No visible output from sleep 5 command

We can pass durations to sleep in days, hours, and minutes, as well as in seconds. To do this include  a suffix of either d, h, m, or s with the duration. To cause sleep to pause for one day, four hours, seven minutes and five seconds, use a command like this:

我们可以以天,小时和分钟为单位,以秒为单位传递sleep时间。 为此,请在持续时间后加上d, h, m,s后缀。 要使睡眠暂停一天,四个小时,七分钟零五秒,请使用以下命令:

sleep 1d 4h 7m 5s

The s suffix (for seconds) is optional. With no suffix, sleep will treat any duration as seconds. Suppose you wanted to have sleep pause for five minutes and twenty seconds. One correct format of this command is:

s后缀(秒)是可选的。 没有后缀, sleep会将任何持续时间视为几秒钟。 假设您想让sleep暂停五分钟二十秒。 此命令的一种正确格式是:

sleep 5m 20

If you forget to provide the m suffix on the minutes duration, you will instruct sleep to pause for five seconds and then again for twenty seconds. So sleep will pause for 25 seconds.

如果忘记在分钟数持续时间上提供m后缀,则会指示sleep暂停五秒钟,然后再暂停二十秒钟。 因此, sleep将暂停25秒。

Many commands require you to provide parameters in a specific order, but sleep is very forgiving. You can provide them in any order and sleep will make sense out of them. You can also provide a floating point number as a parameter. For example, 0.5h is a valid way to indicate you wish sleep to pause for half an hour.

许多命令要求您以特定顺序提供参数,但是sleep是非常宽容的。 您可以以任何顺序提供它们,而sleep将使它们变得有意义。 您还可以提供浮点数作为参数。 例如,0.5h是指示您希望sleep暂停半小时的有效方法。

All of the following (increasingly eccentric) commands tell sleep to pause for 10 seconds.

以下所有(越来越古怪的)命令都告诉sleep暂停10秒钟。

sleep 10
sleep 5 5s
Sleep 1 1 1s 1 1 1s 1 2
sleep 0.16667m

在命令前使用睡眠暂停 (Using Sleep to Pause Before a Command)

The sleep command can be used to give a pause before the execution of a command. This command would pause for 15  seconds and then give a bleep.

sleep命令可用于在命令执行前暂停。 此命令将暂停15秒,然后发出提示音。

sleep 15 && echo -en '\007'

使用睡眠在两个命令之间暂停 (Using Sleep to Pause Between Two Commands)

You can use sleep to give 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:

您可以使用sleep在两个命令之间暂停。 此命令将列出您的Documents目录中的文件,暂停五秒钟,然后将当前工作目录更改为您的主目录:

ls -R ~/Documents && sleep 5 && cd ~
bash脚本 sleep_如何使用Linux Sleep命令暂停Bash脚本_第2张图片

使用睡眠暂停脚本执行 (Using Sleep to Pause Execution of a Script)

You can use the sleep command in shell scripts to pause execution of the script for a precise amount of time. Typically, you’d do this to allow some process sufficient time to complete before the script continues its processing. You can also use it to rate-limit the requests a script makes to another resource.

您可以在外壳程序脚本中使用sleep命令在准确的时间内暂停脚本的执行。 通常,您会这样做,以便在脚本继续处理之前,有一些过程有足够的时间来完成。 您还可以使用它来限制脚本对另一个资源的请求的速率。

To demonstrate exactly that, here is a script that calls out to a Google web service using curl. When you query the web service with the ISBN number of a book, it responds with a dump of JSON data regarding that book. We can parse that data by passing it through the jq utility to retrieve the title of the book. So that the script doesn’t stress the web service, it sleeps for one second between web requests.

为了确切说明这一点,以下是一个脚本,该脚本使用curl调出Google Web服务。 当用一本书的ISBN号查询Web服务时,它会以与该书有关的JSON数据转储作为响应。 我们可以通过将数据传递给jq实用程序来解析该数据,以检索该书的书名。 为了使脚本不会对Web服务造成压力,它会在两次Web请求之间Hibernate一秒钟。

Create a file containing the following text, and save it as check_book.sh.

创建一个包含以下文本的文件,并将其另存为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."

Type the following command to set the execution 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 onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.

该脚本需要curljq实用程序。 如果您使用的是Ubuntu或其他基于Debian的发行版,请使用apt-get将这些软件包安装到系统上。 在其他Linux发行版上,请改用Linux发行版的程序包管理工具。

sudo apt-get install curl
sudo apt-get install jq

Create a text file containing the following numbers, and save it as books.txt.

创建一个包含以下数字的文本文件,并将其另存为books.txt

9781565921276
9781874416685
9781565921672
9780521431088
9781491941591

Run the check_book.sh script and pass in the books.txt file as a parameter.

运行check_book.sh脚本,并将books.txt文件作为参数传递。

./check_book.sh books.txt
bash脚本 sleep_如何使用Linux Sleep命令暂停Bash脚本_第3张图片

The requests are made to the Google web service at one second intervals. The title of the book will appear shortly after each ISBN number is queried.

每隔一秒就会向Google网络服务发送一次请求。 查询每个ISBN号后不久,该书的标题就会出现。

That’s all there is to sleep. The inner workings of the check_book.sh script are beyond the scope of this article. The script was chosen purely to illustrate a valid use of the sleep command. If you wish to read more about the two main components of the script, refer to the  curl project page and the  jq on-line manual.

这就是sleep的全部。 check_book.sh脚本的内部工作超出了本文的范围。 选择该脚本纯粹是为了说明sleep命令的有效用法。 如果您想阅读更多有关脚本的两个主要组件的信息,请参考curl 项目页面和jq 在线手册 。

翻译自: https://www.howtogeek.com/410299/how-to-pause-a-bash-script-with-the-linux-sleep-command/

bash脚本 sleep

你可能感兴趣的:(linux,java,shell,大数据,python)