Php system() / exec() don't return output

15,554

It sounds like the program is outputting its warnings to standard error rather than standard output. exec will only catch standard output. I don't know for certain that standard error is always sent to the apache error log, but it seems likely.

If you don't need compatibility with non-*nix systems, you can redirect standard error to standard output by appending 2>&1 to the command:

exec('some_command --option 2>&1', $output, $ret);

This should both make the warnings available to your php program and prevent unnecessary logging.

Share:
15,554
HappyDeveloper
Author by

HappyDeveloper

Updated on June 04, 2022

Comments

  • HappyDeveloper
    HappyDeveloper almost 2 years

    For common commands like 'ls', exec() works fine, eg:

    exec('ls',$output,$retval);
    var_dump($output,$retval);
    // $output contains an array of filenames, and retval === 0
    

    But when trying to execute another program, I can't get the output:

    exec('some_command --a_parameter',$output,$retval);
    var_dump($output,$retval);
    // $output contains an empty array, end $retval === 0
    

    This command prints some lines when executing it directly from the command-line though. I know the command has been successful because of the results (some files updated, data added, etc), and yet I can't see the output.

    Any ideas?

    • Khez
      Khez almost 13 years
      Does the command you issue actually print any output ?
    • HappyDeveloper
      HappyDeveloper almost 13 years
      It does when doing it directly from the command line. I mean I can see some lines of text appearing in the screen. Maybe there are different types of output?
    • Charles
      Charles almost 13 years
      What is the actual command you're running, please?
    • HappyDeveloper
      HappyDeveloper almost 13 years
      its just a program I downloaded, not a linux thing
  • D T
    D T over 9 years
    add 2>&1 but output still is array empty, if execute from cmd , it output 2 values
  • intuited
    intuited over 9 years
    Hmm could be that the command only outputs if its output is directed to a terminal. What are you trying to do?