tags:

views:

188

answers:

4

I am trying to execute a Perl script like so:

/usr/bin/ec2-consistent-snapshot 'vol-dr3131c2'

When the Perl script fails it 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?

+2  A: 

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.

manyxcxi
Will try that out. Any idea why PHP can't capture the output? I have a feeling that the error message is not going to stdout.
Marcel Tjandraatmadja
manyxcxi
A: 

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');
}
mobrule
No, it doesn't. In fact, it has very poor facilities for running other programs (and a significant bug in 5.2: http://bugs.php.net/bug.php?id=44994)
Colin Fine
mobrule
A: 

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.

intuited
A: 

Perl scripts die with their messages going to stderr, so you will need to capture stderr as well as stdout to see what happened.

Ether