1

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
The error messages are probably going to stderr and PHP is only receiving stdout. I just threw that code up there quickly without thinking about it differently, but you could probably remove the '> /tmp/exec.out' section and just have it redirect stderr to stdout '2>&1'. If that works, you won't be left w/ a tmp file and PHP /SHOULD/ capture the error message.
0

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');
}

2 Comments

No, it doesn't. In fact, it has very poor facilities for running other programs (and a significant bug in 5.2: bugs.php.net/bug.php?id=44994)
It turns out that on my planet, PHP does have backticks: google.com/search?hl=en&q=PHP+backticks .
0

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.