Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

command line - Php system() / exec() don't return output

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...