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

I'm using system to call an application as a child process. If the child process does not create a file within a certain amount of time, I want the parent to kill the child process. This *appears* to work, except that because the child is waiting for input, I have to hit enter on the keyboard before I actually get control back (I'm using "$SIG{INT} = sub { die };" to kill the process as it *seems* to work better than kill 1, 6, or 9). When running as a batch job, it just hangs. Any suggestions would be appreciated. TIA
  • Comment on Cannot kill child process when it's waiting for response

Replies are listed 'Best First'.
Re: Cannot kill child process when it's waiting for response
by graff (Chancellor) on Aug 04, 2002 at 01:16 UTC
    Given that the child process is expecting to read something on stdin, you should start it with "open()" rather than with "system()" -- this gives the parent process the ability to continue opertation after the child is opened/started, print data to the child process if/when needed, and also provides the parent with the child's process-id, if you want that, though you could probably just "close" it -- e.g.
    my $child_pid = open( CHILD, "| child_program arg1 arg2" ) || die "unable to start child_program\n"; # Feed the child, if that's necessary select CHILD; $|++; # turn off output buffering print CHILD $/; # send a blank line # give the child about 5 seconds to produce something my $counter = 0; until ( -s "child_output.file" or ++$counter == 5 ) { sleep 1; } # now, we could kill $child_pid, or just: close CHILD;
    Depending on what the child is actually supposed to be doing, there may be some traps here. Definitely read "perldoc -f open" and "perldoc perlipc".