perl invokes shell-- interrupt ^C stops the shell, not the perl
I want to use a Perl script to batch a rep开发者_开发知识库etitive operation which is invoked with system(). When something goes wrong and I want to interrupt this script, the ^C is captured by the shell, stopping whatever job, and the Perl script goes merrily along to the next one.
Is there a way I can invoke the job so that an interrupt will stop the Perl script?
You can check $?
to see whether the command executed by system died from signal 2 (INT
):
Here's a full example of parsing $?
:
my $rc=system("sleep 20");
my $q=$?;
if ($q == -1) {
print "failed to execute: $!\n"
} elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($q & 127), ($q & 128) ? 'with' : 'without';
} else {
printf "child exited with value %d\n", $q >> 8;
}
# Output when Ctrl-C is hit:
# child died with signal 2, without coredump
Therefore the exact check you want is:
my $rc=system("sleep 20");
my $q=$?;
if ($q != -1 && (($q & 127) == 2) && (!($? & 128))) {
# Drop the "$? & 128" if you want to include failures that generated coredump
print "Child process was interrupted by Ctrl-C\n";
}
References: perldoc system for $?
handling and system()
call; perldoc perlvar for more details on $?
You are not checking the return value of system
. Add to your parent program:
use autodie qw(:all);
and it program will abort as expected:
"…" died to signal "INT" (2) at … line …
You may catch this exception with Try::Tiny in order to clean-up on your own or use a different message.
精彩评论