STDOUT and STDERR redirection fails for forked process

Discussion in 'Perl Misc' started by Peter, Jul 19, 2007.

  1. Peter

    Peter Guest

    Hi,

    Read the manpages, googled around, found some samples and followed
    those to get it to work... but still fails..

    What do i want? Simply, fork a command using the OPEN instruction,
    redirect STDERR to STDOUT and capture the returncode of the executed
    command.
    I wrote the code wich is printed below, which works fine. Only output
    from STDERR is not redirected to STDOUT but printed to the terminal
    and the returncode is not captured. (So somehow redirected to STDOUT
    but outside the open statement).

    e.g. when the routine executes 'ls -Q' which sends the errormessage to
    STDERR and a RC=2, the output from STDERR is printed to the terminal
    and the returncode ($?) is 0.

    Any ideas? suggestions?

    Peter

    Jul19 10:01:40 INF Data received from client "05794A6E5CD1122F ls -Q"
    Jul19 10:01:40 DBG subroutine loccmd (15) (ls -Q)
    Jul19 10:01:40 DBG Issuing command 'ls -Q'
    ls: Not a recognized flag: Q
    Usage: ls [-1ACFLNRabcdefgilmnopqrstuxE] [File...]
    Jul19 10:01:40 DBG leaving subroutine loccmd passing values '0' '' ''


    eval {
    local $SIG{ALRM} = sub { die "Timed Out!\n" } ;
    # set the alarm
    alarm($timeout);

    ## Issue the command by forking the process and monitoring the
    output
    if (open STATUS, "$cmdstr 2>&1 |") {
    while (<STATUS>) {
    $result .= $_ ;
    }
    $errmsg = $! ;
    $rcverror = $? ;

    close STATUS ;
    }
    else {
    $errmsg = "Fork failed for \'$cmdstr\'" ;
    $rcverror = -2 ;
    }

    # reset the alarm
    alarm(0) ;
    $rcvtimeout = 0 ;
    } ;
     
    Peter, Jul 19, 2007
    #1
    1. Advertising

  2. Peter

    -berlin.de Guest

    Peter <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > Read the manpages, googled around, found some samples and followed
    > those to get it to work... but still fails..
    >
    > What do i want? Simply, fork a command using the OPEN instruction,
    > redirect STDERR to STDOUT and capture the returncode of the executed
    > command.
    > I wrote the code wich is printed below, which works fine. Only output
    > from STDERR is not redirected to STDOUT but printed to the terminal
    > and the returncode is not captured. (So somehow redirected to STDOUT
    > but outside the open statement).
    >
    > e.g. when the routine executes 'ls -Q' which sends the errormessage to
    > STDERR and a RC=2, the output from STDERR is printed to the terminal
    > and the returncode ($?) is 0.
    >
    > Any ideas? suggestions?
    >
    > Peter
    >
    > Jul19 10:01:40 INF Data received from client "05794A6E5CD1122F ls -Q"
    > Jul19 10:01:40 DBG subroutine loccmd (15) (ls -Q)
    > Jul19 10:01:40 DBG Issuing command 'ls -Q'
    > ls: Not a recognized flag: Q
    > Usage: ls [-1ACFLNRabcdefgilmnopqrstuxE] [File...]
    > Jul19 10:01:40 DBG leaving subroutine loccmd passing values '0' '' ''


    This is not the output of the code you showed. How can we help
    you when you don't give us consistent information?

    > eval {
    > local $SIG{ALRM} = sub { die "Timed Out!\n" } ;
    > # set the alarm
    > alarm($timeout);
    >
    > ## Issue the command by forking the process and monitoring the
    > output
    > if (open STATUS, "$cmdstr 2>&1 |") {
    > while (<STATUS>) {
    > $result .= $_ ;
    > }
    > $errmsg = $! ;
    > $rcverror = $? ;
    >
    > close STATUS ;
    > }
    > else {
    > $errmsg = "Fork failed for \'$cmdstr\'" ;
    > $rcverror = -2 ;
    > }
    >
    > # reset the alarm
    > alarm(0) ;
    > $rcvtimeout = 0 ;
    > } ;


    That code is a mess. It isn't strict-safe. Why is everything wrapped
    in an eval block? What is the value of $timeout? Or is it $rcvtimeout?
    You are accessing $? too early. The return code is only put there after
    close().

    Reducing your code to the essentials (something *you* should have done),
    this is what remains:

    my $cmdstr = 'ls -9'; # -Q is a valid flag in some systems
    open STATUS, '-|', "$cmdstr 2>&1";
    print ">>> $_" while <STATUS>;
    close STATUS;
    print "retcode: $?\n";
    exit;

    This prints:

    >>> ls: invalid option -- 9
    >>> Try `ls --help' for more information.

    retcode: 256

    Note the ">>>" in front of the error message. It shows that the error
    is not printed to stdout by the command but is caught and printed by
    the perl program. The effect you complain about simply isn't there.

    So what is this all about?

    Anno
     
    -berlin.de, Jul 19, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    689
    Roman Neuhauser
    Apr 4, 2005
  2. subrahmanya
    Replies:
    1
    Views:
    461
  3. unbewust
    Replies:
    6
    Views:
    257
    unbewust
    Aug 11, 2007
  4. Zbigniew Fiedorowicz

    Getting STDERR from forked child processes?

    Zbigniew Fiedorowicz, Feb 5, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    187
    Aaron Sherman
    Feb 5, 2004
  5. Peter
    Replies:
    1
    Views:
    118
    Brian McCauley
    Jul 20, 2007
Loading...

Share This Page