简体   繁体   English

我的被​​叫脚本中有“捕获'echo ignore'USR1”的字样,为什么调用脚本被杀死?

[英]I have “trap 'echo ignore' USR1” in my called script, why does the calling script get killed?

Say I have these two bash scripts: 说我有以下两个bash脚本:

/tmp/trapper: 的/ tmp /捕手:

#!/bin/bash
trap 'echo trapper: ignoring USR1' USR1
"$(dirname $0)"/usr1er & p=$!
sleep 1
echo trapper: now killing usr1er
kill $p
echo trapper: sleeping
sleep 1
echo trapper: reached end of trapper

/tmp/usr1er: 的/ tmp / usr1er:

#!/bin/bash
trap 'echo "usr1er: EXIT received, sending USR1"; kill -USR1 0' EXIT
while sleep 1;do echo usr1er: sleeping;done

trapper is supposed to trap USR1 and simply ignore it. 捕获程序应该捕获USR1并直接忽略它。 It starts usr1er, which kills its process group with the USR1 signal. 它启动usr1er,并使用USR1信号终止其进程组。 Now, if I start trapper as a script on its own from an interactive shell, it kills usr1er and exits normally: 现在,如果我从交互式shell本身以脚本形式启动trapper,它将杀死usr1er并正常退出:

$ /tmp/trapper; echo done
trapper: now killing usr1er
trapper: sleeping
usr1er: EXIT received, sending USR1
/tmp/trapper: line 9: 16596 Terminated              "$(dirname $0)"/usr1er
trapper: ignoring USR1
trapper: reached end of trapper
done

While if I try $(/tmp/trapper) , it exits the whole shell. 如果我尝试$(/tmp/trapper) ,它将退出整个外壳。 Similarly, if I make a separate script that calls /tmp/trapper , like /tmp/outer : 同样,如果我制作一个单独的脚本来调用/tmp/trapper ,如/tmp/outer

#!/bin/bash
"$(dirname $0)"/trapper
echo outer: reached end of outer

it gets killed without printing the "reached end of outer": 它被杀死而没有打印“外部的到达端”:

$ /tmp/outer
trapper: now killing usr1er
trapper: sleeping
usr1er: EXIT received, sending USR1
User defined signal 1
/tmp/trapper: line 9: 23544 Terminated              "$(dirname $0)"/usr1er
User defined signal 1
trapper: ignoring USR1
trapper: reached end of trapper

Why? 为什么?

It seems that $() does not start the process with a separate process group / PGID (apparantly for making Cc work). 看来, $() 以单独的进程组/ PGID启动过程(apparantly制作Cc工作)。

Also, any non-interactive shell will also not start separate PGID's for their children (unless you turn on job control with set -m): 同样,任何非交互式外壳程序也不会为其子级启动单独的PGID(除非您使用set -m打开作业控制):

$ bash -c '/tmp/trapper;echo done'
trapper: now killing usr1er
trapper: sleeping
usr1er: EXIT received, sending USR1
User defined signal 1
$ /tmp/trapper: line 9: 17522 Terminated              "$(dirname $0)"/usr1er
trapper: ignoring USR1
trapper: reached end of trapper

Note that "done" is not printed, the outer bash script, which doesn't trap USR1, is killed while trapper keeps on living until the end. 请注意,“ done”未打印,不会捕获USR1的外部bash脚本会被杀死,而trapper会一直活到最后。

You can check the PGID of each process by putting ps -o %p%r%c -p$$ in the scripts: 您可以通过在脚本中添加ps -o %p%r%c -p$$来检查每个进程的PGID:

$ /tmp/outer
  PID  PGID COMMAND
27630 27630 outer
  PID  PGID COMMAND
27633 27630 trapper
  PID  PGID COMMAND
27635 27630 usr1er
trapper: now killing usr1er
trapper: sleeping
usr1er: EXIT received, sending USR1
User defined signal 1
$ /tmp/trapper: line 9: 27635 Terminated              "$(dirname $0)"/usr1er
trapper: ignoring USR1
trapper: reached end of trapper

Try this change 试试这个变化

/tmp/usr1er: 的/ tmp / usr1er:

#!/bin/bash
trap 'echo "usr1er: EXIT received, sending USR1"; kill -USR1 $PPID' TERM
while sleep 1;do echo usr1er: sleeping;done

Handled TERM signal instead of EXIT & sent USR1 to $PPID instead of 0 处理了TERM信号而不是EXIT,并将USR1发送到$PPID而不是0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从bash脚本发送kill -s USR1会停止该过程,而从终端执行此操作不会 - Sending kill -s USR1 from bash script stops the process while doing the same from terminal does not 为什么等待完成的父 shell 进程不能可靠地接收从 Bash 脚本中的后台作业发送的 USR1 信号? - Why may USR1 signals sent from background jobs in a Bash script not be reliably received by the parent shell process waiting for their completion? 使用陷阱USR1时Bash随机函数的行为 - Bash random function behavior when using trap USR1 如何获取ruby`print`来呼应bash脚本? - How to I get ruby `print` to echo to calling bash script? 为什么我的 Expect 脚本只回显未运行的命令? - Why does my Expect script only echo the command not running? 我希望我的脚本从字面上回显“ $ 1”到文件中 - I want my script to echo “$1” into a file literally 为什么我的脚本额外增加0 - Why do I get an extra 0 on my script 为什么我的 bash 脚本中不断出现语法错误? 它是否与“:”有关? - Why do I keep getting syntax error in my bash script? Does it have something related to the “:”? 为什么 echo "$@" 不显示脚本名称,但显式迭代却显示? - Why does echo “$@” not show the script name but explicit iteration does? 为什么我必须输入enter才能退出bash脚本? - Why do I have to type enter to exit my bash script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM