在关闭文件句柄时向perl脚本发送信号

这个问题在这里已经有了答案:

  • 发送给包含子项的perl脚本时,SIGINT(^ C)会发生什么情况? 3个答案

  • 我无法重现你正在观察的行为。 当我在终端中按CTRL-C时,孩子和家长立即收到SIGINT

    use diagnostics;
    use feature qw(say);
    use strict;
    use warnings;
    
    $SIG{INT} = sub {  say "This is expected to print"; die };
    my $pid = open ( my $pipe, "|-", "script.pl" );
    say "PID = $pid";
    eval {
        say "Closing..";
        my $close_ok = close $pipe; # Note "close" here waits for child to exit
        if ( ! $close_ok ) {
            say "Error closing: $!";
        }
        else {
            say "Close done.";
        }
    };
    if ( $@ ) {
        say "Parent caught SIGINT.";
    }
    

    其中script.pl是:

    #! /usr/bin/env perl
    
    use feature qw(say);
    use strict;
    use warnings;
    
    $SIG{INT} = sub { die };
    eval {
        say "Sleeping..";
        for (1..5) {
            sleep 1;
            say $_;
        }
    };
    if ( $@ ) {
        say "Child caught SIGINT.";
        exit;
    }
    

    运行终端中的第一个程序(Ubuntu 16.04上的gnome-terminal )的输出是:

    PID = 1746
    Closing..
    Sleeping..
    1
    2
    ^CThis is expected to print
    Child caught SIGINT.
    Parent caught SIGINT.
    Uncaught exception from user code:
        refcnt: fd -1 < 0
    

    请注意,有一个未捕获的异常refcnt: fd -1 < 0 。 我不知道那是什么。 也许因为close没有成功?

    链接地址: http://www.djcxy.com/p/33705.html

    上一篇: Sending a signal to a perl script while it is closing a filehandle

    下一篇: Using marble testing rxjs5 method inside another project