
OK, that’s enough computer time. You can give processes time limits, setting a maximum time they can run for with the timeout
command. Here’s a tutorial to putting limits on running programs with this command.
好,足够的计算机时间。 您可以给进程指定时间限制,使用timeout
命令设置它们可以运行的最长时间。 这是使用此命令限制运行程序的教程。
超时对您有什么帮助? (What Does timeout Do For You?)
The timeout
command allows you to set a limit on the length of time a program will run for. But why would you want to do that?
使用timeout
命令可以设置程序运行时间的限制 。 但是为什么要这么做呢?
One case is when you know exactly how long you want a process to run for. A common use-case is to have timeout
control a logging or data-capture program so that the log files don’t relentlessly devour your hard drive space.
一种情况是,您确切地知道一个进程要运行多长时间。 一个常见的用例是让timeout
控制日志记录或数据捕获程序,以使日志文件不会无休止地占用您的硬盘空间。
Another case is when you don’t know how long you want a process to run for, but you do know you don’t want it to run indefinitely. You might have a habit of setting processes running, minimizing the terminal window, and forgetting about them.
另一种情况是,您不知道一个进程要运行多长时间,但是您知道您不希望该进程无限期地运行。 您可能习惯于设置进程运行,最小化终端窗口并忘记它们。
Some programs–even simple utilities—can generate network traffic at levels that can impede the performance of your network. Or they can tie up the resources on a target device, slowing down its performance. (ping
, I’m looking at you.) Leaving these types of programs running for extended periods while you’re away from your computer is bad practice.
某些程序(甚至是简单的实用程序)可能会以一定程度的网络生成流量,从而影响网络性能。 或者他们可以占用目标设备上的资源,从而降低其性能。 ( ping
,我在看你。)在离开计算机的同时长时间运行这些类型的程序是一种不好的做法。
timeout
is part of the GNU Core Utils so Linux and Unix-like operating systems such as macOS all have timeout built right in. There’s nothing to install; you can use it right out of the box.
timeout
是GNU Core Utils的一部分,因此Linux和类似 macOS的类Unix操作系统都内置了超时功能。 您可以直接使用它。
超时入门 (Getting Started With timeout)
Here’s a simple example. For example, with its default command-line options, the ping
command will run until you stop it by hitting Ctrl+C. If you don’t interrupt it, it’ll just keep going.
这是一个简单的例子。 例如,使用其默认命令行选项, ping
命令将一直运行,直到您通过按Ctrl + C停止它为止。 如果您不中断它,它将继续前进。
ping 192.168.4.28

By using timeout
, we can make sure ping
doesn’t run on and on, chewing up network bandwidth and pestering whatever device is being pinged.
通过使用timeout
,我们可以确保ping
不会持续运行,从而浪费了网络带宽,并且缠扰着正在被ping的任何设备。
This next command uses timeout
to time-limit ping
. We’re allowing 15 seconds of run time for ping
.
下一个命令使用timeout
来限制ping
。 我们允许ping
运行15秒。
timeout 15 ping 192.168.4.28

After 15 seconds timeout
terminates the ping
session and we are returned to the command line prompt.
15秒钟后, timeout
终止ping
会话,然后我们将返回命令行提示符。

将超时与其他时间单位一起使用 (Using timeout With Other Time Units)
Note that we didn’t have to add an “s” behind the 15. timeout
assumes the value is in seconds. You could add an “s,” but it really makes no difference.
请注意,我们不必在15后面添加“ s”。 timeout
假定该值以秒为单位。 您可以添加“ s”,但实际上没什么区别。
To use a time value measured in minutes, hours or days add an “m,” an “h,” or a “d.”
要使用以分钟,小时或天为单位的时间值,请添加“ m”,“ h”或“ d”。
To have ping run for three minutes, use the following command:
要运行ping三分钟,请使用以下命令:
timeout 3m ping 192.168.4.28

ping
will run for three minutes before timeout
steps in and halts the ping
session.
ping
将运行三分钟,然后timeout
,然后暂停ping
会话。

通过超时限制数据捕获 (Limiting Data Capture With timeout)
Some data capture files can grow big very quickly. To prevent such files becoming unwieldy or even problematic in size, limit the amount of time the capture program is allowed to run.
一些数据捕获文件可能会很快变得很大。 为防止此类文件变得笨拙甚至大小问题,请限制允许运行捕获程序的时间。
In this example, we’re using tcpdump
, a network traffic capture tool. On the test machines that this article was researched on, tcpdump
was already installed in Ubuntu Linux and Fedora Linux. It had to be installed on Manjaro Linux and Arch Linux, with the following command:
在此示例中,我们使用网络流量捕获工具tcpdump
。 在研究本文的测试机器上, tcpdump
已安装在Ubuntu Linux和Fedora Linux中。 必须使用以下命令将其安装在Manjaro Linux和Arch Linux上:
sudo pacman -Syu tcpdump

We can run tcpdump
for 10 seconds with its default options, and redirect its output to a file called capture.txt with the following command:
我们可以使用其默认选项将tcpdump
运行10秒钟,然后使用以下命令将其输出重定向到一个名为capture.txt的文件:
timeout 10 sudo tcpdump > capture.txt

(tcpdump
has its own options to save captured network traffic to a file. This is a quick hack because we’re discussing timeout
, not tcpdump
.)
( tcpdump
有其自己的选项将捕获的网络流量保存到文件中。这是一个快速的技巧,因为我们正在讨论timeout
,而不是tcpdump
。)
tcpdump
starts capturing network traffic and we wait for 10 seconds. And 10 seconds comes and goes and tcpdump
is still running, and capture.txt is still growing in size. It’s going to take a hasty Ctrl+C to halt tcpdump
.
tcpdump
开始捕获网络流量,我们等待10秒钟。 十秒钟过去了, tcpdump
仍在运行,capture.txt的大小仍在增长。 急需按Ctrl + C来停止tcpdump
。
Checking the size of capture.txt with ls
shows that it grew to 209K in a matter of seconds. That file was growing fast!
用ls
检查capture.txt的大小表明,它在几秒钟内增长到了209K。 该文件增长很快!
ls -lh capture.txt

What happened? Why didn’t timeout
stop tcpdump
?
发生了什么? 为什么timeout
没有停止tcpdump
?
It’s all to do with signals.
这与信号有关。
发送正确的信号 (Sending The Right Signal)
When timeout
wants to stop a program it sends the SIGTERM signal. This politely asks the program to terminate. Some programs may choose to ignore the SIGTERM signal. When that happens, we need to tell timeout
to be a little more forceful.
当timeout
要停止程序时,它将发送SIGTERM信号 。 礼貌地要求程序终止。 某些程序可能选择忽略SIGTERM信号。 发生这种情况时,我们需要让timeout
更有力。
We can do that by asking timeout
to send the SIGKILL signal instead.
我们可以通过请求timeout
来发送SIGKILL信号来实现。
The SIGKILL signal cannot be “caught, blocked or ignored”—it always gets through. SIGKILL doesn’t politely ask the program to stop. SIGKILL hides around the corner with a stopwatch and a cosh.
SIGKILL信号不能被“捕获,阻止或忽略”,它始终会通过。 SIGKILL不会有礼貌地要求程序停止。 SIGKILL带着秒表和Cosh躲在拐角处。
We can use the -s
(signal) option to tell timeout
to send the SIGKILL signal.
我们可以使用-s
(信号)选项来告诉timeout
发送SIGKILL信号。
timeout -s SIGKILL 10 sudo tcpdump > capture.txt

This time, as soon as 10 seconds have elapsed, tcpdump
is stopped.
这次,一旦经过10秒钟, tcpdump
就会停止。

先礼貌地问 (Asking Politely First)
We can ask timeout
to try to stop the program using SIGTERM, and to only send in SIGKILL if SIGTERM didn’t work.
我们可以要求timeout
尝试使用SIGTERM停止程序,并且仅在SIGTERM无法正常工作时才发送SIGKILL。
To do this, we use the -k
(kill after) option. The -k
option requires a time value as a parameter.
为此,我们使用-k
(之后杀死)选项。 -k
选项需要一个时间值作为参数。
In this command, we’re asking timeout
to let dmesg
run for 30 seconds, and to then terminate it with the SIGTERM signal. If dmesg
is still running after 40 seconds, it means the diplomatic SIGTERM was ignored and timeout
should send in SIGKILL to finish the job.
在此命令中,我们要求timeout
让dmesg
运行30秒,然后使用SIGTERM信号终止它。 如果dmesg
在40秒后仍在运行,则意味着外交SIGTERM被忽略, timeout
应发送SIGKILL以完成工作。
dmesg
is a utility that can monitor the kernel ring buffer messages and display them in a terminal window.
dmesg
是一个实用程序,可以监视内核环形缓冲区消息并将其显示在终端窗口中。
timeout -k 40 30 dmseg -w

dmesg
runs for 30 seconds and stops when it receives the SIGTERM signal.
dmesg
运行30秒,并在收到SIGTERM信号时停止。

We know it wasn’t SIGKILL that stopped dmesg
because SIGKILL always leaves a one-word obituary in the terminal window: “Killed.” That didn’t happen in this case.
我们知道不是SIGKILL停止了dmesg
因为SIGKILL总是在终端窗口中留下一个单词的itu告:“杀死”。 在这种情况下,这没有发生。
检索程序的退出代码 (Retrieving the Program’s Exit Code)
Well-behaved programs pass a value back to the shell when they terminate. This is known as an exit code. Typically this is used to tell the shell–or whatever process launched the program— whether problems were encountered by the program as it ran.
行为良好的程序在终止时会将值传递回外壳。 这称为退出代码。 通常,它用于告知外壳程序或启动程序的任何进程,程序运行时是否遇到问题。
timeout
provides its own exit code, but we may not care about that. We are probably more interested in the exit code from the process that timeout
is controlling.
timeout
提供了自己的退出代码,但我们可能对此并不在意。 我们可能对timeout
控制的过程中的退出代码更感兴趣。
This command lets ping
run for five seconds. It is pinging a computer called Nostromo, which is on the test network that was used to research this article.
此命令可让ping
运行五秒钟。 它正在对名为Nostromo的计算机执行ping操作,该计算机位于用于研究本文的测试网络上。
timeout 5 ping Nostromo.local

The command runs for five seconds and timeout
terminates it. We can then check the exit code using this command:
该命令运行五秒钟, timeout
终止它。 然后,我们可以使用以下命令检查退出代码:
echo $?

The exit code is 124. This is the value timeout
uses to indicate the program was terminated using SIGTERM. If SIGKILL terminates the program, the exit code is 137.
退出代码为124。这是timeout
值,用于指示使用SIGTERM终止了程序。 如果SIGKILL终止程序,则退出代码为137。
If we interrupt the program with Ctrl+C the exit code from timeout
is zero.
如果我们用Ctrl + C中断程序,则timeout
退出代码为零。
timeout 5 ping Nostromo.local
echo $?

If the execution of the program ends before timeout
terminates it, timeout
can pass the exit code from the program back to the shell.
如果程序的执行在 timeout
终止之前结束,则timeout
可以将退出代码从程序传递回Shell。
For this to happen the program must come to a halt of its own accord (in other words, it is not terminated by timeout
), and we must use the --preserve-status
option.
为此,程序必须自行停止(换句话说,它不会因timeout
终止),并且我们必须使用--preserve-status
选项。
If we use the -c
(count) option with a value of five ping
will only fire off five requests. If we give timeout
a duration of one minute, ping
will have definitely terminated by itself. We can then check the exit value using echo
.
如果我们将-c
(计数)选项的值ping
5 ping
则只会触发五个请求。 如果我们将timeout
时间设置为一分钟,则ping
肯定会自行终止。 然后我们可以使用echo
检查退出值。
timeout --preserve-status 1m ping -c 5 Nostromo.local
echo $?

ping
completes its five ping requests and terminates. The exit code is zero.
ping
完成其五个ping请求并终止。 退出代码为零。
To verify the exit code is coming from ping
, let’s force ping
to generate a different exit code. If we try to send ping requests to a non-existent IP address, ping
will fail with an error exit code. We can then use echo
to check that the exit code is non-zero.
为了验证退出代码是否来自ping
,让我们强制ping
生成另一个退出代码。 如果我们尝试将ping请求发送到不存在的IP地址,则ping
将失败,并显示错误退出代码。 然后,我们可以使用echo
检查退出代码是否为非零。
timeout --preserve-status 1m ping -c 5 NotHere.local
echo $?

The ping
command obviously cannot reach the non-existent device, so it reports the error and closes down. The exit code is two. This is the exit code ping
uses for general errors.
ping
命令显然无法到达不存在的设备,因此它报告错误并关闭。 退出代码是两个。 这是ping
用于一般错误的退出代码。
制定基本规则 (Setting Ground Rules)
timeout
is all about providing some boundaries to running programs. If there’s a danger the log files might overrun your hard drive or that you might forget you left a network tool running, wrap them in timeout
and let your computer self-regulate.
timeout
就是为运行程序提供一些边界。 如果存在危险,日志文件可能会超出硬盘驱动器的范围,或者您可能忘记了网络工具仍在运行,请将它们包装为timeout
并让计算机进行自我调节。
翻译自: https://www.howtogeek.com/423286/how-to-use-the-timeout-command-on-linux/