PHP Exec() Not Returning Error Message in Output

PHP exec() not returning error message in output

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

How to retrieve PHP exec() error responses?

You can receive the output result of the exec function by passing an optional second parameter:

exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess',$output);
var_dump($output);

PHP: Why isn't exec() returning output?

You should redirect stderr to stdout.

To do that, change your exec() call like this:

exec("ping -c 1 $domain_bad 2>&1", $output, $return_var);

More info about 2>&1 meaning here.

PHP exec command has no effect despite returning no error code

Check the $output.

This is probably a path or access right issue. For example if you run this script with the web-server priviliges it probably doesn't have the right to alter the files.

php exec: does not return output

There are a few posts to the relevant sections of the PHP Manual such as this one:

I was having trouble using the PHP exec command to execute any batch
file. Executing other commands (i.e., "dir") works fine). But if I
executed a batch file, I receieved no output from the exec command.

The server setup I have consists of Windows Server 2003 server running
IIS6 and PHP 5.2.3. On this server, I have:

  1. Granted execute permissions to the Internet User on c:\windows\system32\cmd.exe.
  2. Granted Everyone->Full Control to the directory in which the batch file is written.
  3. Granted Everyone->Full Control on the entire c:\cygwin\bin directory and its contents.
  4. Granted the Internet User "log on as batch" permissions.
  5. Specified the full path to each file being executed.
  6. Tested these scripts running from the command line on the server and they work just fine.
  7. Ensured that %systemroot%\system32 is in the system path.

It turns out that even with all of the above in place on the server, I
had to specify the full path to cmd.exe in the exec call.

When I used the call: $output = exec("c:\\windows\\system32\\cmd.exe
/c $batchFileToRun");

then everything worked fine. In my situation, $batchFileToRun was the
actual system path to the batch file (i.e., the result of a call to
realpath()).

There are a few more on both the exec and shell_exec manual pages. Perhaps following through them will get it up and working for you.

How to ignore errors in shell_exec?

You just have to discard error output by redirecting stderr to /dev/null

$str = shell_exec("command 2>/dev/null");

Non-error output - stdout - will be stored into $str as before.


Note that you don't need to suppress errors on shell_exec with the @ operator or wrap it into a try-catch block since shell_exec doesn't fail (you don't have a PHP runtime error).

It is the command it is asked to execute that may generate errors, the above approach will suppress those in the output.

Also some-command > /dev/null 2>&1 as other suggested is not what you want (if I understood correctly your question) since that would discard both error and non-error output.


A final note: you can decide to catch/discard stdout and/or stderr.

Of course you have to rely upon the fact that the command you're running send regular output to stdout and errors to stderr. If the command is not compliant to the standard (ex. sends everything to stdout) you're out of luck.

exec() error responses

You should redirect stderr to stdout somehow like this

$stout = exec($command . " 2>&1", $output, $status);

See also here
PHP StdErr after Exec()

PHP misses system variables when using exec

After re-installing laragon it worked. I Have checked before uninstalling path variables and i haven't seen mine that should be in.

I couldn't modify it either so I just reinstalled it.



Related Topics



Leave a reply



Submit