简体   繁体   English

如何清除由父进程设置的子进程中的陷阱?

[英]How to clear a trap in a child process which was set by a parent process?

Process A sets a trap. 进程A设置陷阱。 It then creates a child process B. How can I clear the trap set by process A? 然后,它将创建一个子进程B。如何清除进程A设置的陷阱?

processA 工艺A

#! /bin/bash
# processA.sh
trap '' 15
sh processB.sh

processB 进程B

#! /bin/bash
# processB.sh    
echo "Current trap"
trap -p    
echo "Clearing trap 15"
trap - 15    
echo "New trap"
trap -p

Output 输出量

Current trap
trap -- '' TERM
Clearing trap 15
New trap
trap -- '' TERM

In above example, I am clearing a trap in the child process B but it is not getting cleared. 在上面的示例中,我正在清除子进程B中的陷阱,但是没有清除它。 Operating system is Linux. 操作系统是Linux。

In general, you cancel a trap for signal 15 with trap 15 . 通常,用trap 15消除信号15的trap 15 That would include resetting an ignored signal back to the default when the current shell started ignoring it. 这将包括在当前外壳程序开始忽略它时将忽略的信号重置为默认值。

However, some experimentation (with Bash 3.2.57 on macOS Sierra 10.12) shows that if the signal was ignored when the process was started (inherited from the parent) then you can't reset to the default handling — it continues to be ignored. 但是,一些实验(在macOS Sierra 10.12上使用Bash 3.2.57)显示,如果在启动进程时(从父级继承)忽略了该信号,那么您将无法重置为默认处理-它将继续被忽略。 Further, attempts like trap ': do nothing' 15 followed by trap 15 do not do the job. 此外,像trap ': do nothing' 15以及trap 15之后的尝试都无法完成任务。

I wrote a C program called iqon (interrupt, quit on — first version in 1989) to ensure that signals were trapped (or ignored) by the command that it then executed. 我编写了一个名为iqon的C程序(中断,退出-1989年的第一个版本),以确保信号被执行的命令捕获(或忽略)。

iqon -s 15 -- bash processB.sh

Variations on this theme work. 有关此主题的作品有所不同。

Note that a simple-minded version of iqon which only sets signal SIGTERM to the default status (no argument handling) could be as simple as: 请注意,仅将信号SIGTERM设置为默认状态(无参数处理)的iqon思维简单版本可能很简单:

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    if (argc <= 1)
    {
        fprintf(stderr, "Usage: %s cmd [arg ...]", argv[0]);
        return 1;
    }
    signal(SIGTERM, SIG_DFL);
    execvp(argv[1], &argv[1]);
    perror(argv[1]);
    return 1;
}

Clearly, you can make it more complex without any difficulty. 显然,您可以毫无困难地使其变得更加复杂。 My version supports a help message like this: 我的版本支持这样的帮助消息:

Usage: iqon [-dhV] [-s signal] [-i signal] cmd [args ...]
  -d         Set interrupt and quit to default
  -h         Print this help and exit
  -i signal  Ignore signal (number)
  -s signal  Set default for signal (number)
  -V         Print version information and exit

The default (setting interrupt and quit to the default behaviour) is a legacy from its original purpose — for running programs from within programs created by a 4GL where the interrupt and quit signals were ignored. 默认设置(将中断和退出设置为默认行为)是其原始目的的遗产-用于在由4GL创建的程序中运行程序,而忽略了中断和退出信号。 It's also where the name came from: i nterrupt and q uit on . 它的名称也来自此地: i nterrupt和q uit on

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM