4

I'm trying to use shell_exec() and having hard time understanding how to use it. I referred to this page: Is there a way to use shell_exec without waiting for the command to complete?.

I created two PHP files:

shell_exec.php

<?php 
    shell_exec('php ' . __DIR__ . '/log.php > /dev/null 2>/dev/null &');
?>

log.php

<?php
    $file = __DIR__ . '/log.txt';
    $current = time() . ": sample text\r\n";
    file_put_contents($file, $current,FILE_APPEND);
?>

Then I accessed the page of shell_exec.php from my browser. I expected that it runs log.php and creates a file. However, nothing happens.

I suspect that the php paths specified in the function, which is php in the above code, is different in different operating system. I'm currently testing in Windows, so that may be the reason that it doesn't work. But I'm not 100% sure.

If so, how can I find the php path and specify it in my environment? Also does that mean that if I distribute a script which uses shell_exec() and call a php script, do I have to write some code to find out the php path? In that case, is there a good way of doing it?

Community
  • 1
  • 1
Teno
  • 2,414
  • 3
  • 30
  • 54

4 Answers4

3

If so, how can I find the php path and specify it in my environment? Also does that mean that if I distribute a script which uses shell_exec()...

Unfortunately, yes. The environment where you distribute the script must have a command line PHP (which is not necessarily the same as the PHP used by the Web server - it might be a different version).

And there is no easy way of "finding" it.

So what you would need to do is, you clearly state (when distributing) that PHP command line executable must be in the PATH.

At that point, shell_exec('php -q -f yourfile...') ought to work.

You can redirect the output to a file (always the same if you want) and delete it if there are no errors. Otherwise the file will show what errors occurred, which may be useful.

UPDATE: and the first times, to debug, don't redirect the output, but try:

<?php
    Header("Content-Type: text/plain");
    error_reporting(E_ALL);
    print shell_exec('php "' . __DIR__ . '/log.php" 2>&1'); // > /dev/null 2>/dev/null &');
?>

This way you can see if the command hits some other snag, such as not having the rights to create the log.txt file in the first place.

LSerni
  • 49,775
  • 9
  • 56
  • 97
  • I got `'php' is not recognized as an internal or external command, operable program or batch file.` – Teno Sep 12 '12 at 22:07
  • Euh. Then it means it is not in the system PATH (or maybe it's not even installed: having Webserver PHP doesn't imply having Commandline PHP). Check you do have the PHP binary, and then add the appropriate path to your system configuration. – LSerni Sep 12 '12 at 22:10
  • Finally `shell_exec('Z:\xampp\php\php.exe "' . __DIR__ . '/log.php" 2>&1');` worked. Thanks. But is it possible to find the path programmatically? – Teno Sep 12 '12 at 22:18
  • 1
    As I read the link dbf posted, it seems to be available since PHP v5.4. – Teno Sep 12 '12 at 22:29
  • 1
    It is not (easily) possible, because the "sure way" would be to search *the whole disk* with `opendir()` and `file_exists()`. If it is in the system PATH it works automatically, if it is not you can't know where it is. Say that you installed XAMPP and also Wamp-X and also the PHP MSI package: you get three different PHP.EXE -- which is the one you want? There is no function that can tell you that, and ay, there's the rub. – LSerni Sep 12 '12 at 22:35
1

If your files are in a directory tree containing a space, you have a problem:

shell_exec('php ' . __DIR__ . '/log.php > /dev/null 2>/dev/null &');

becomes:

shell_exec('php somepath/with spaces in it/log.php > /dev/null 2>/dev/null &');

which makes php run somepath/with as a script with the arguments:

'spaces', 'in', 'it/log.php'

try putting quotes around your argument:

shell_exec('php "' . __DIR__ . '/log.php" > /dev/null 2>/dev/null &');
Bob Fincheimer
  • 17,134
  • 1
  • 25
  • 52
  • Indeed there was a space in the directory path. And I tried to put the double colons as you suggest but still not working. Thanks for the answer. – Teno Sep 12 '12 at 22:08
0

have you tried the commandline that you expect shell_exec to execute ?

I think you missed the '-f' in that line

-2

This looks like a really bad idea. Why are you using shell_exec to call php? It sounds like you're testing from a browser, and calling via another PHP file, so you know there's a working PHP interpreter in the server environment.

Instead of shell_exec(), you should include the file and use it directly.

You won't have to worry about the path of the PHP binary then, and you'll actually get more control over the file.

ernie
  • 6,164
  • 20
  • 27