2

I'm trying to make a module in Drupal which utilizes exec(). The script runs fine if I load it manually in the browser or when I run it as su -l www-data, but when run by Drupal as a module the exec function doesn't run. I'm wondering how I can get more data out of this to better debug, or if anyone might know why Drupal won't let the exec run.

When loaded by Drupal, the entire script does execute without giving me any warnings or errors, which I find weird. If I add an or die('could not exec'); after the exec(), when I load my drupal page it does show me just "could not exec" and nothing else.

Any ideas? Thanks!

[EDIT]

I got this to working by specifying the explicit path of the php file I'm trying to exec. That is, I changed: exec('php mymodule.helper',$output);

to

exec('php /var/www/mydrupal/modules/mymodule/mymodule.helper',$output);

[EDIT2]
Actually, I changed it now to:
exec ('php ' . __DIR__ . '/mymodule.helper',$output);

Which works perfectly.

5
  • what are you trying to execute? Commented Nov 1, 2010 at 21:41
  • Specifically, exec('php mymodule.helper',$output); Commented Nov 1, 2010 at 21:43
  • if you add "2>&1" to the command, what does it say? Commented Nov 1, 2010 at 22:02
  • It gave me an error saying it couldn't load mymodule.helper, and prepending the filename with its absolute path fixed everything! Firstly, can you tell me what the 2>&1 does, and secondly, why does DIR show the directory the file I'm trying to exec is in, but trying to exec it doesn't work? Commented Nov 2, 2010 at 1:30
  • 2>&1 redirects STDERR to STDOUT, which is what exec() captures. 2 is the error message, &1 is a reference to the main output, and > does redirection. Does dir show the module directory when run via exec()? Normally in Drupal that would show the site's base directly, since everything runs through index.php. Commented Nov 3, 2010 at 0:36

2 Answers 2

1

I suggest you start by narrowing down the problem to a) something in non-CLI PHP, b) something in Drupal, or c) something in the command running via exec.

Some tests:

  • Is this really Drupal-specific? Try copying some exec() code to a .php file and load it directly (outside Drupal). Does it work any better there? (Note: your comparison between CLI non-Drupal and non-CLI Drupal isn't quite the same as comparing Drupal and non-Drupal.)
  • Is non-CLI PHP running in safe mode? (I'd put my bet on this being the issue.) You can check this with echo(ini_get('safe_mode'));.
  • What is the actual return variable from exec()? Just checking if it's true or false isn't very helpful; being a string, it's almost always true. Try $result = exec(...); print_r($result);
Sign up to request clarification or add additional context in comments.

1 Comment

Safe mode is off (at least it shows as off in phpinfo(); however when I try to do the echo(ini_get('safe_mode')), nothing is output. Setting $result=exec() and then print_r()'ing the $result outputs nothing.
0

exec ('php ' . DIR . '/mymodule.helper',$output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.