If I have a long-running system
command like apt-cache search
, is there a way to forward a SIGINT
sent via ^C
Make the child the head of a process group, then send the signal to the whole process group.
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use POSIX qw( setpgid );
my $child_pid;
$SIG{INT} = sub {
kill INT => -$child_pid if $child_pid;
exit(0x80 | 2); # 2 = SIGINT
};
my $child_pid = fork();
if (!$child_pid) {
setpgid($$);
exec 'apt-cache', 'search', 'hi';
}
WAIT: {
no autodie;
waitpid($child_pid, 0);
redo WAIT if $? == -1 and $!{EINTR};
die $! if $? == -1;
}
exit( ( $? >> 8 ) | ( 0x80 | ( $? & 0x7F ) ) );