为什么`timeout 2 timeout 1 bash`卡住了

why does `timeout 2 timeout 1 bash` stuck

正在研究Linuxshell中的timeout命令。

当我尝试 timeout 1 bash 时,bash 将 运行 并在 1 秒后被杀死。

当我尝试 timeout 2 timeout 1 yes 时,程序 yes 将 运行 持续 1 秒并被第二个 timeout 杀死。

但是当我尝试 timeout 2 timeout 1 bash 时,它卡住了。没有 bash shell 出现,即使我按 Ctrl+C 也保持 运行ning。

我知道一个命令写两个timeout是没有用的

我只是想知道为什么会这样。

这是相同行为的另一个示例:

strace timeout 1 bash 即使你打断了strace,bash也会继续运行ning.

如果我们同时跟踪 bash 进程本身,我们会注意到以下循环。

--- SIGTTIN {si_signo=SIGTTIN, si_code=SI_USER, si_pid=7162, si_uid=1000} --- rt_sigaction(SIGTTIN, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f097723a7e0}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f097723a7e0}, 8) = 0 ioctl(255, TIOCGPGRP, [6412])
= 0 rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f097723a7e0}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f097723a7e0}, 8) = 0 kill(0, SIGTTIN) = 0

现在,根据http://www.gnu.org/software/libc/manual/html_node/Job-Control-Signals.html

Macro: int SIGTTIN

A process cannot read from the user’s terminal while it is running as a background job. When any process in a background job tries to read from the terminal, all of the processes in the job are sent a SIGTTIN signal. The default action for this signal is to stop the process. For more information about how this interacts with the terminal driver, see Access to the Terminal.

timeout 2 timeout --foreground 1 bash 有效,因为内部超时将允许它与 tty 一起工作,尽管它不是 运行 直接来自交互式 shell.

人工超时

   --foreground

          when not running timeout directly from a shell prompt,

          allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out

我们可以根据需要链接任意多个超时,只要来自交互式 shell 的 运行 之外的所有超时都带有 --foreground 选项:

timeout 3 timeout --foreground 2 timeout --foreground 1 bash

另请查看 man bash 中的 SIGNALS 部分,了解有关 bash 在不同情况下如何对各种信号做出反应的更多信息。