title: PHP CGI replaces name of called file in command line exec calls, causing infinite loop
body: <p>I'm transfering an application to a new server. The application uses command line calls, and the new server runs PHP as CGI/FastCGI.</p>
  
  <p>Problem: The script that gets executed on command line is not the file mentioned in the exec command, but the calling file itself.</p>
  
  <p>The application uses exec() to run scripts on the command line. Entire code of calling_script.php (scaled down from actual application to demonstrate the issue) is:</p>
  
  <pre><code>&lt;?
  echo __FILE__;
  $test = 1;
  shell_exec("/usr/bin/php /path/called_script.php ".$test." &gt;&gt; /path/out.log 2&gt;&amp;1 &amp;");
  </code></pre>
  
  <p>For the called_script.php it simply is:</p>
  
  <pre><code>&lt;?
  echo __FILE__;
  var_dump($argv[1]);
  </code></pre>
  
  <p>What does happen is: calling_script.php is called, instead of called_script.php - and the Parameter gets lost as well - calling_script.php gets called in an infinite loop, as each call to it causes another exec call that is executed on it again.
  I tried passthrough and shell_exec instead of exec - with the same results.</p>
  
  <p>I found this explanation in a forum:
  <a href="http://www.mombu.com/php/php-5-forum/t-24759-exec-or-system-et-all-cause-an-infinite-loop-of-starting-requested-program-8469354.html" rel="nofollow noreferrer">http://www.mombu.com/php/php-5-forum/t-24759-exec-or-system-et-all-cause-an-infinite-loop-of-starting-requested-program-8469354.html</a>
  (via <a href="https://stackoverflow.com/questions/3615713/exec-cause-an-infinite-loop-of-starting-requested-program">exec() cause an infinite loop of starting requested program</a> )</p>
  
  <p>Quote: "You are running the CGI version of PHP and it picks up the script name
  from the environment now, rather than from the command line as it is
  "more secure".</p>
  
  <p>There is (or should be) an ini setting to control this, however, you
  probably want to be running the CLI version of PHP from your scripts
  instead."</p>
  
  <p>I tried to get my hands on the CLI version of PHP - but the new server is managed by a provider and has no CLI version. Are there any other solutions?</p>
  
  <p>Eventually, what I want to achieve is asynchonous execution of php - any recommendation on how i might achieve that without the CLI version of PHP? Use CURL, PEAR or fsockopen? Queue or task queue servers (such as gearman or beanstalkd) and if so which one? Any solution that I can implement quickly is heartily welcome.</p>