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

bash – 子进程中的陷阱信号

发布时间:2020-12-15 18:39:06 所属栏目:安全 来源:网络整理
导读:在子进程/后台进程中运行时,我无法捕获信号. 这是我简单的bash脚本: #!/bin/bashecho "in child"trap "got_signal" SIGINTfunction got_signal { echo "trapped" exit 0}while [ true ]; do sleep 2done 当运行这个和以后做 kill -SIGINT (pid) 一切都符合
在子进程/后台进程中运行时,我无法捕获信号.

这是我简单的bash脚本:

#!/bin/bash

echo "in child"

trap "got_signal" SIGINT

function got_signal {
  echo "trapped"
  exit 0
}

while [ true ]; do
    sleep 2
done

当运行这个和以后做

kill -SIGINT (pid)

一切都符合预期,它打印“被困”并退出.

现在,如果我从父脚本启动相同的脚本,如下所示:

#!/bin/bash

echo "starting the child"

./child.sh &

然后孩子不会陷入信号….?

更改后使用SIGTERM而不是SIGINT,它似乎正常工作?

OSX上的bash联机帮助页(但是在其他版本中应该是一样的)有关于信号处理的说法:

Non-builtin commands run by bash have signal handlers set to the values
inherited by the shell from its parent. When job control is not in
effect,asynchronous commands ignore SIGINT and SIGQUIT in addition to
these inherited handlers.

进一步,在陷阱命令下:

Signals ignored upon entry to the shell cannot
be trapped or reset.

由于脚本默认情况下不使用作业控制,所以这就是您正在谈论的情况.

(编辑:李大同)

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

    推荐文章
      热点阅读