How can I run a system command and die if anything is written to STDERR?

后端 未结 2 597
广开言路
广开言路 2021-01-23 19:23

I\'m writing a Perl script which uses an external script. The external script must run from a specific directory so I found the following useful:

use IPC::System         


        
相关标签:
2条回答
  • 2021-01-23 19:51

    This is covered in the Perl FAQ.

    Presuming test_app is a program that outputs one line to stdout and one line to stderr:

    use IPC::Open3;
    use Symbol 'gensym';
    
    my($wtr, $rdr, $err);
    $err = gensym;
    
    my $pid = open3($wtr, $rdr, $err, 'test_app');
    
    waitpid($pid, 0);
    my $status = $? >> 8;
    
    my $stdout = <$rdr>;
    my $stderr = <$err>;
    
    print "out output: $stdout\n";
    print "err output: $stderr\n";
    
    print "Exit code: $status\n";
    

    EDIT: Per the request updated to include capturing the exit code. You could also have asked perldoc IPC::Open3 which says

    waitpid( $pid, 0 );
    my $child_exit_status = $? >> 8;
    

    And which you should read anyway for its cautions and caveats.

    0 讨论(0)
  • 2021-01-23 20:01

    If significant output is being written to stdout and/or stderr or you're both reading and writing to the process. You need to be a lot more careful with your I/O handling to avoid various blocking problems.

    my ($wtr, $rdr, $err) ;
    
    my $pid = IPC::Open3::open3($wtr, $rdr, $err, @_);
    
    close($wtr);
    
    my $stdout = '';
    my $stderr = '';
    
    my $s = IO::Select->new;
    
    $s->add($rdr) if $rdr;
    $s->add($err) if $err;
    
    while (my @ready = $s->can_read) {
    
        foreach my $ioh (@ready) {
    
            my $bytes_read = sysread($ioh, my $chunk = '', 1024);
    
            die "read error: $!" unless $bytes_read >= 0;
    
            if ($bytes_read) {
               ($ioh eq $rdr? $stdout: $stderr) .= $chunk;
            }
        else {
                $s->remove($ioh);
            }
        }
    } 
    
    my $pid1;
    for (;;) {
    
        last if kill(0, $pid);
    
        $pid1 = wait();
        #
        # Wait until we see the process or -1 (no active processes);
        #
        last if ($pid1 == $pid || $pid1 <= 0);
    }
    

    Finish reading before you shutdown the process. If you're writing to the process's stdin, you'd also need to add $wtr and syswrite to the above select loop.

    EDIT

    Rationale:

    The above is probably overkill for simple cases. This advanced handling of input and output comes into play when you're likely to move more than a few K of data.

    You wouldn't need it if you were executing a 'df' command for example.

    However, it's when system buffers for any of stdin, stdout or stderr fill up that blocking becomes likely and things can get more involved.

    If the child process fills up the stderr and/or stdout buffers, it'll likely block and wait for you to clear them. But if you're waiting for the process finish before you read from stdout or stderr; thats a deadlock. You'll likely to see that the system call never finishes and the child process never completes.

    There's a similar possibility of deadlock if stdin is being written to, but the child process is unable to consume the input. This is particularly likely in a 'pipe' situation where the child process is consuming input and writing to stdout.

    The select loop is about progressively clearing the buffers to avoid blocking. Both stdout and stderr are monitored concurrently.

    If you're writing to stdin and reading from stdout (a pipe), you'll want to keep stdout and stderr clear and only write to stdin when its ready to receive input.

    Simply waiting for the process to finish, then reading stdout/stderr probably works 90% of the time. This reply is just to give you somewhere to go if things get more complicated and processes start to block or go into deadlock.

    EDIT2

    As for which to use, I'd say start simple, test hard.

    Go with Sorpigal's approach, but try to stress test with higher data volumes and under more difficult loads and conditionals that you'd ever expect in a live system.

    0 讨论(0)
提交回复
热议问题