Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec() not returning error message in output

Tags:

php

xml

svn

exec

I am trying to get certain output for svn command in XML format. Output is ok when I type valid parameters. However, when I type in wrong password, output does not show error message. This is the PHP code:

exec('/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/', $output); 

Here is output I get in the terminal:

<?xml version="1.0"?> <log> svn: OPTIONS of 'http://a51.unfuddle.com/svn/a51_activecollab': authorization failed: Could not authenticate to server: rejected Basic challenge (http://a51.unfuddle.com) 

And here is the output I get from the $output variable with var_dump:

array(2) { [0]=> string(21) "<?xml version="1.0"?>" [1]=> string(5) "<log>" } 

As you can see the $output variable does not return third line of output, where terminal does. Please help me to get the same output as I get in terminal (I even tried with shell_exec() or system() methods but they return the same output as exec()) How do I get full output? Thank you in advance!

like image 700
Goran Avatar asked Oct 05 '10 12:10

Goran


People also ask

Why Exec() is not working in PHP?

If the answer above could not solve your problem, maybe exec () is disabled. You can try to check php.ini file at disable_functions line. Good hint.

Why is exec() not working in PHP on OpenBSD?

In the default configuration from OpenBSD, PHP runs into a chroot. So the exec () command will not work. You will get a 127 (command not found) result code. The reason is, the shell (/bin/sh) is missing in chroot, but the exec () command requires the shell. (I have noticed this on OpenBSD 7.0 with PHP 8.0.11.)

Why exec() command does not work?

So the exec () command will not work. You will get a 127 (command not found) result code. The reason is, the shell (/bin/sh) is missing in chroot, but the exec () command requires the shell. (I have noticed this on OpenBSD 7.0 with PHP 8.0.11.) result_code -1 could mean "Maximum number of file descriptors reached".

Why is my PHP program running in the background?

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends. On Windows exec () will first start cmd.exe to launch the command.


1 Answers

You need to capture the stderr too.

Redirecting stderr to stdout should do the trick. Append 2>&1 to the end of your command.

e.g.

exec("/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/ 2>&1", $output); 

If it is a complex command (e.g. one with a pipe, like doing a mysqldump and passing it to gzip and then redirecting to file mysqldump ... | gzip > db.sql.gz) create a subshell to capture the overall standard-error and redirect it to standard-output:

exec('( error_command | cat >/dev/null ) 2>&1', $output) #     ^                                ^   ^ #     `-- sub-shell with the command --´   `-- stderr to $output 
like image 58
racetrack Avatar answered Sep 23 '22 01:09

racetrack