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

Hi, I again seek your wisdom. An external program which I call via IPC::Run (run \@args or die $?) seemingly finishes its calculations but does not communicate its status. I cant find it in the running processes but perl just hangs there waiting for the external to come back. What options do I have to ask the status of the external program and continue after it finishes. Thank you
  • Comment on External program called via system does not come back

Replies are listed 'Best First'.
Re: External program called via system does not come back
by RuntimeError (Acolyte) on Feb 13, 2014 at 15:51 UTC
    Just to report back what I come up with (ideas stolen from Perl Cookbook and the Perl command reference)
    use strict; use warnings; use IPC::Run; use POSIX qw(:signal_h :errno_h :sys_wait_h); use Config; ... my $has_nonblocking = $Config{d_waitpid} eq "define" || $Config{d_wait +4} eq "define"; if ($has_nonblocking) { $SIG{CHLD} = \&REAPER; sub REAPER { my $pid = waitpid(-1, &WNOHANG); # wait for any child, return PID +if state changed else return 0 if stopped or -1 if error if ($pid == -1) { # ignore } elsif ( &WIFEXITED($?) ) { # true if exited normally print "$pid exited.\n"; } else { # must have been some stop signal or no change in state print "False alarm $pid.\n"; } $SIG{CHLD} = \&REAPER; } my $pid = fork(); defined $pid or die $!; unless ($pid) { my @args = ( ... ); run \@args or die $?; exit; } my $kid; do { $kid = waitpid(-1, &WNOHANG); } while $kid > 0; } ... exit;
    Please comment and give advice. Thank you.
Re: External program called via system does not come back
by graff (Chancellor) on Feb 14, 2014 at 02:50 UTC
    I'm not sure whether this is relevant, but just in case… depending on your version of perl, it's at or near the end of the man page for fork:

    Note that if your forked child inherits system file descriptors like STDIN and STDOUT that are actually connected by a pipe or socket, even if you exit, then the remote server (such as, say, a CGI script or a backgrounded job launched from a remote shell) won't think you're done. You should reopen those to /dev/null if it's any issue.

Re: External program called via system does not come back
by Anonymous Monk on Feb 13, 2014 at 08:15 UTC
      Send to background... this is very good idea. I'll try this. Thank you much.