简体   繁体   中英

Why exit is not always called in perl?

I have a perl script for setting up a remote host. And this is its interrupt handler in case if something will go wrong:

sub interrupt
{
    $SIG{'__DIE__'} = '';
    my $signal = shift;
    print STDERR "Error $SELF_NAME: Bootstrapping of host '$REMOTE_HOST' is interrupted with error '$signal', deleting remote temporary directory $REMOTE_TEMP_DIR.\n";
    remote_exec("perl -e \"use File::Path; rmtree('$REMOTE_TEMP_DIR');\"", $REMOTE_HOST, $REMOTE_EXEC_METHOD, $REMOTE_EXEC_PORT, $USERNAME, $PASSWORD, 0, 1);
    exit 1;
}

And this handler is always called when there is a need. So I can see the error about interrupted bootstrapping in STDERR . But exit 1; is not called and script returns with exit_code = 0 . BUT if I add this line print STDERR "After remote_exec and before exit"; between last two lines of my handler it works fine (ie returns with exit_code = 1 ).

Note: remote_exec just calls system($COMMAND) inside as I'm testing it on a local host.

UPDATE

Adding some details about how the script is being called:

I run the script from my C++ program which tracks its standard logs and checks exit status and in case when exit status is not equal to 0 it prints some error. So, when I add some extra line in my script between exit and system I can see the error my C++ program prints, but if there is not such extra line the C++ program tells that the script is successfully exited, which means that exit status is 0 .

You didn't actually demonstrate the problem, so I had to guess at how to demonstrate it, and failed.

$ perl -e'
   sub interrupt {
      $SIG{"__DIE__"} = "";
      my $signal = shift;
      print STDERR "...\n";
      system("echo ...");
      exit 4;
   }
   $SIG{INT} = \&interrupt;
   <>;
'
^C...
...

$ echo $?
4

(Used 4 cause it's more distinctive than 1.)

What do you even mean by "not called"? You seem to indicate the program did exit as a result of the interrupt, which means it got called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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