1

I'd like to do a similar thing than How to "fork" a video conversion process into background, in php? :

exec("/usr/bin/php ./foo.php > /dev/null 2>&1 &");  // executed in Apache

However, this will run on a variety of platforms : my machine, where php is compiled in ~/, windows, several prod servers...

Is there a way to programmatically get the "/usr/bin/php" part ?

Things I could think of :

  • get the current pid, guess executable from it
  • in C, the first argument is the path to the executable, maybe there is something similar
Community
  • 1
  • 1
Calvin1602
  • 9,413
  • 2
  • 44
  • 55
  • If you expect to use this code in a webpage request (you tagged it apache), you'll never know. Apache isn't calling any PHP binary in the common configurations. – Dan Grossman Feb 09 '11 at 10:23
  • @Dan What are "common configurations?" suPHP? That's called using binaries (php-cgi). – Linus Kleen Feb 09 '11 at 10:26
  • PHP as a module, not as a CGI, is the default and most common configuration. PHP code runs under the apache process. – Dan Grossman Feb 09 '11 at 10:28

1 Answers1

2

Easiest way is to simply make sure the php CLI binary is found in $PATH environment variable, and then just use php /path/to/foo.php.

On UNIX you can also use /usr/bin/env php which will execute the first php binary found in $PATH. But that obviously won't work in Windows.

Lastly one obvious way is to have the location to PHP as a configurable option, and use the user-specified path when appropriate.

reko_t
  • 55,302
  • 10
  • 87
  • 77
  • ok, that's what I feared. Since php can refer to either php4 or php5 (I need php5) I went the config way. Thanks. – Calvin1602 Feb 09 '11 at 13:17