On Tue, Aug 17, 2004 at 05:38:53PM -0400, Jim wrote:
> > My perl script forks and the child is subsequently replaced by another
> > (non-Perl) program with exec(). That child has its own subprocess.
> >
> > In response to certain events, the parent tries to kill the child (and
> > its children).
> >
> > Currently, my method for doing this is:
> >
> > local $SIG{HUP} = 'IGNORE';
> > kill HUP => -$$;
> >
> > This works fine when the parent is run directly from a command prompt;
> > however, when the parent is itself called from a shell script, the
> > child's children are not killed.
> Can you show the rest of the code? There is documentation about this in the
> Perl Cookbook, if you have access to that.
Yes, I have the Perl Cookbook. I think I've read and tried all the
relevant recipes from there, but I'm still unable to kill grandchildren
when the child's output is redirected to /dev/null and the parent is run
from a shell script.
Here's the basic code simplified a bit for illustration--I don't think it will help
too much:
my $pid;
unless ($pid = fork()) {
/mp3$/i and exec($mp3_player . " \"$_\"" . ($verbose ? "" : " > /dev/null 2>&1"));
}
my $kid;
ReadMode 3;
do {
my $key = ReadKey(0.01) || "";
$kid = waitpid(-1,WNOHANG) || 0;
if ($key eq "") {
} elsif ($key_next =~ /\Q$key\E/) {
print "Skipping to next song...\n" unless $quiet;
local $SIG{HUP} = 'IGNORE'; # don't want to kill ourselves here!
kill HUP => -$$; # killing a negative pid kills whole
process group; shell and player
} elsif ($key_quit =~ /\Q$key\E/) {
print "Quitting...\n" unless $quiet;
local $SIG{HUP} = 'IGNORE';
kill HUP => -$$;
last;
}
} until ($kid > 0);
The problem here is the kill HUP => -$$ only works when the parent is run
from the command prompt, not when run from another shell script.
--
Adam Kessel
http://adam.rosi-kessel.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>