dcorbin has asked for the wisdom of the Perl Monks concerning the following question:

I have a program A. It launches (fork/exec) program B, which is a perl script. The perl script, in turn, launches program C using system(). I have problem that when A sends a signal (HUP, TERM or QUIT) to B, if C is running, it continues to run. I want to stop both B & C "on demand". I have tried using the following snipit in B. C seems to faithfully respond to HUP everytime I've tried.
sub cleanup { $SIG{HUP}='IGNORE'; kill (HUP,-$$); exit -1; } $SIG{HUP}='cleanup'; $SIG{TERM}='cleanup'; $SIG{QUIT}='cleanup';
I have very repeatable situation where C will not terminate when B does. I've tried a number of variations unsuccessfully. Ideas?

Replies are listed 'Best First'.
Re: Signal Handling, with system
by graff (Chancellor) on Apr 29, 2002 at 03:45 UTC
    I tried to replicate (as simply as possible) the situation you described, which I think would go like this (using "find" as "program C" -- I'm running linux too):
    #!/usr/bin/perl # PROGRAM "A" my $pid = fork(); if ( $pid ) { while (1) {sleep 3} } else { exec( "/tmp/junk.perl" ) } __END__ #!/usr/bin/perl # PROGRAM "B" system( "find / -ls > /tmp/junk.find 2>&1" ); # this takes a while __END__

    The only way I could make it so that "program C" ("find") kept running after ctl-C stopped "A" was if I added "&" in the system call to run C in the background:
    system( "find / -ls > /tmp/junk.find 2>&1 &" );

    In this case, of course, the "B" process becomes defunct very soon (it exits after launching "C" in the background), so no amount of signal handling in perl would stop C, unless I took steps to make C's pid available to A, and kill it from there.

    You didn't say much about the system call issued in B to launch C; does it background C? Are you sure B is still running when you halt A?

    Would it be practical for B to open( C, "process_C |"); or something like that? This way, when B closes the file handle, C is terminated.

Re: Signal Handling, with system
by dcorbin (Sexton) on Apr 28, 2002 at 17:48 UTC
    One point I forgot to mention, is this is on a Linux system.