Hi -

I'm trying to execute PHP script 2 from within PHP script 1 - the idea is that script 2 will carry on processing data until it has finished doing so, even if the user logs off or browses to a different part of the website (hosted by DreamHost, though this is probably of no relevance).

The main problem I am encountering is that if you do the following (the 'bash -c etc.' part serves to background the process and return control to PHP - see http://uk2.php.net/manual/en/function.exec.php) -

exec('bash -c "exec nohup setsid /usr/local/bin/php /path/to/php/script/2.php > /path/to/output/file.txt 2>&1 &"');

  • then file.txt will end up containing the output of script 1 - NOT script 2. I have tried to find some information on this, but found only one mention of a similar problem, and no real solution. The same issue arises if you try to execute the script from within a bash script and use exec to call sh, rather than calling PHP.

This issue does not arise when script 1 is called from the command line - under those circumstances, script 2 produces the expected output.

Any suggestions? I could use cron, but would prefer to be able to call script 2 when the user prompts it.

    25 days later

    I have the same problem posted.

    Basically it seems that if I'm trying to invoke a php page from the web browser it only calls the original.

    IE in the previous example, script 2 is never called, script 1 is called again. In fact if you were to cause it not to wait on the completion of the script by adding '&' after your output file, it would result in an infinite loop of the page calling itself.

    If anyone has encountered this problem, any insight would be helpful.

      I've recreated the problem with as little code as possible.

      Script #1 - Call.php

      <?php
              exec('/usr/local/bin/php Test.php > Fork.log');
              echo "Call.php";
      ?>

      Script #2 - Test.php

      <?php
              echo "Test.php";
      ?>

      The results of Fork.log are as follows:

      When Call.php is run from command line

      X-Powered-By: PHP/4.4.7
      Content-type: text/html

      Test.php

      When Call.php is run from a web browser

      X-Powered-By: PHP/4.4.7
      Content-type: text/html

      Call.php


      I've tried running a shell script which calls the php with the same results, works in command line, not in the browser.

      The basic behavior seems to be that any php invoked as a result of exec, shell_exec etc, is calling the original page (Script 1/Call.php) instead of the page that's actually passed (Script 2/Test.php). If I run the exec() in the background with

      exec('/usr/local/bin/php Test.php > Fork.log &');

      Call.php calls itself infinitely. (at which point I have to toss an exit() at the top of the file to get it to stop.)

      I've seen a lot of examples for forking while trying to resolve this problem and it doesn't seem to be the syntax but more likely a configuration issue. I'm on a shared host so have little control over the environment, but if I can find the problem might be able to get it fixed.

      Any help would be greatly appreciated.

        I haven't found a solution to the problem as such - I settled for using wget to work around it. In other words, I now use

        exec("wget http://url/to/php/script.php > /dev/null &");

        • which works, as it happens, but would be no good if the script were located in a directory not accessible to the web server. The "> /dev/null &" ensures that wget carries on downloading even if the page from which script.php was originally called is closed by the user, incidentally.
          Write a Reply...