How to retrieve PHP exec() error responses?

Viewed 66136

Below is the command I tried executing, without success:

exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess');

When you add a die() at the end, it catches that there's an error:

exec('ln -s ' . PLUGIN_DIR . '/.htaccess ' . ABSPATH . '/.htaccess') or die('what?!');

For the above exec() statement, a permissions problem is causing the error, but PHP isn't displaying it. How do you display from PHP what error is occurring?

7 Answers

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

This in not a direct answer to your question, but is very useful, if you want to know what exactly happened by returned error code.

If error code ($res), returned by command:

exec('command', $out, $res);

was in range 1-2, 126-165, 255, than it was returned by shell (eg. BASH) invoked from PHP (to execute your command). If returned code is not in this range, than it was returned by your command (not shell).

See (for BASH error code descriptions)

https://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/exitcodes.html

Related