加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 为什么`超时2超时1 bash`卡住了

发布时间:2020-12-14 01:44:44 所属栏目:Linux 来源:网络整理
导读:我正在研究 Linux shell中的timeout命令. 当我尝试超时1 bash时,bash会运行并在1秒后被杀死. 当我尝试超时2超时1是,程序是将运行1秒并被第二次超时杀死. 但是当我尝试超时2超时1 bash时,它被卡住了.即使我按下Ctrl C,也不会显示bash shell并继续运行. 我知道
我正在研究 Linux shell中的timeout命令.

当我尝试超时1 bash时,bash会运行并在1秒后被杀死.

当我尝试超时2超时1是,程序是将运行1秒并被第二次超时杀死.

但是当我尝试超时2超时1 bash时,它被卡住了.即使我按下Ctrl C,也不会显示bash shell并继续运行.

我知道在一个命令中写两个超时是没用的.

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

解决方法

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

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

如果我们在同一时间内对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_restorer=0x7f097723a7e0},8) = 0 ioctl(255,TIOCGPGRP,[6412])
= 0 rt_sigaction(SIGTTIN,{sa_handler=SIG_DFL,
{sa_handler=SIG_IGN,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选项:

超时3超时 – 前景2超时 – 前景1 bash

同时检查man bash中的SIGNALS部分,了解bash在不同情况下如何对各种信号做出反应的更多信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读