views:

43

answers:

2

I can pass back output of the executed script, but I get no error output if the script errors out.

// This is a file that doesn't exists, for testing
$command = './path/to/non/existing/script.sh';

$commandOutput = exec($command, $commandOutput); // works but no error output
//passthru($command, $commandOutput); // works but error output was 127 not file not found
//$commandOutput = escapeshellcmd($command);
echo "The Output:\n|".$commandOutput."|\n";
var_dump($commandOutput);

The Output:

||

I would like the output of the error message:

The Output:

|file not found|

How or what function/parameter would do this?

+2  A: 

You can redirect stderr to stdout so exec() et al will fetch error messages by appending 2>&1 to your command.

see http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

VolkerK
Phill Pafford
if I use this will I loose my server internal logging for the error as well?
Phill Pafford
"server internal logging" ? Do you mean syslog?
VolkerK
yes that's correct, syslog
Phill Pafford
+1  A: 

Try:

ob_start();
passthru($command);
$content_grabbed=ob_get_contents();
ob_end_clean();

echo $content_grabbed;

The second parameter is for the return status of the command sent to the system.

Nick Shepherd
I got an exit code of 127 when trying to run the command eee using passthru, but no error information relating to it from the shell. If you want to detect errors then you need to pass the optional return var(s) when using this function.
pharalia
exec() and passthru() both only "watch" the stdout of the spawned process. So if exec() didn't return the desired output passthru will not "print" it either and the output buffer can't fetch it.
VolkerK
@pharalia what are the 'optional return var(s)' and how do I set them?
Phill Pafford
@nick shepherd I get nothing returned for the error
Phill Pafford
declare a variable and pass it as the second argument to passthru(), it's passed by reference and will be populated with the exit code of the executed application
pharalia