I am trying to execute a Perl script like so:
/usr/bin/ec2-consistent-snapshot 'vol-dr3131c2'
When the Perl script fails 开发者_如何学Cit exits using 'die' and prints out an error message. I can see that error message when executing manually, but I am failing to capture it through PHP.
I tried the following with no success:
exec($command,$output);
echo system($command,$output);
passthru($command);
Any ideas?
You could try:
$command = "/usr/bin/ec2-consistent-snapshot 'vol-dr3131c2' > /tmp/exec.out 2>&1"
exec($command)
echo file_get_contents('/tmp/exec.out');
The '> /tmp/exec.out 2>&1' redirects all output to /tmp/exec.out, then PHP echo's the file back.
Doesn't PHP have backticks?
$output = `$command 2> /tmp/command.err`;
if (looks_like_command_failed($output)) {
$error_message = file_get_contents('/tmp/command.err');
}
The first example given in the documentation for the system() function mentions that it may work in cases where output is not being received by php. The example receives stderr separately from stdout, without using a temp file. The poster doesn't explain why this method would be successful when system() is not. So no answer to the root issue, but maybe a workaround.
Perl scripts die with their messages going to stderr, so you will need to capture stderr as well as stdout to see what happened.
精彩评论