How do I get exec() to display errors?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert S

    #1

    How do I get exec() to display errors?

    I would like to display error messages put out by shell commands. For
    example the following code gives no output and the array $output has no
    values:

    <?php
    exec( 'lss', $output );
    var_dump( $output );
    ?>

    ...assuming that I don't have an executable called 'lss' on my computer.

    The equivalent on the command line displays the error message:

    # php -r 'exec( "lss", $output );'
    # sh: lss: command not found

    How do I get the exec() command to display errors?

    I've tried `exec( 'lss 2/dev/stout', $output );' with no success.

  • Peter van Schie

    #2
    Re: How do I get exec() to display errors?

    Robert S schreef:
    I would like to display error messages put out by shell commands. For
    example the following code gives no output and the array $output has no
    values:
    >
    <?php
    exec( 'lss', $output );
    var_dump( $output );
    ?>
    How about just:
    <?php
    $strRawOutput = exec( 'lss', $output );
    var_dump($strRa wOutput);
    ?>

    This only retrieves the last output line though. If you need more then
    you should use passthru() instead of exec().

    HTH,
    Peter.
    --

    Comment

    • Robert S

      #3
      Re: How do I get exec() to display errors?

      How about just:
      <?php
      $strRawOutput = exec( 'lss', $output );
      var_dump($strRa wOutput);
      ?>
      >
      This still doesn't print the error message. It only prints whatever is
      directed to stdout.

      Comment

      • Jerry Stuckle

        #4
        Re: How do I get exec() to display errors?

        Robert S wrote:
        I would like to display error messages put out by shell commands. For
        example the following code gives no output and the array $output has no
        values:
        >
        <?php
        exec( 'lss', $output );
        var_dump( $output );
        ?>
        >
        ..assuming that I don't have an executable called 'lss' on my computer.
        >
        The equivalent on the command line displays the error message:
        >
        # php -r 'exec( "lss", $output );'
        # sh: lss: command not found
        >
        How do I get the exec() command to display errors?
        >
        I've tried `exec( 'lss 2/dev/stout', $output );' with no success.
        >
        2redirection is a shell function, not the OS. You'll need to use
        shell_exec() to allow the redirection.

        Also, the syntax should be 2>&1. That way if stdout is also redirected
        stderr will follow.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...