PHP exec() as Background Process (Windows Wampserver Environment)

后端 未结 6 1024
-上瘾入骨i
-上瘾入骨i 2020-11-27 13:07

I\'m trying to setup a php trigger file that will set off a background process. (see this question)

I\'m doing this on a Windows Wampserver environment.

So f

相关标签:
6条回答
  • 2020-11-27 13:31

    You may need to change your implementation approach. Having to wait for such a long time would be an annoyance for the user of your app and fatal for the entire app.

    For such tasks, it's usually better to queue the task, ideally on database, and process them periodically. There are chron jobs on Linux based systems. In Windows, you can use a scheduler to launch the backgroundProcess.php.

    0 讨论(0)
  • 2020-11-27 13:36

    Tried to achieve the same on a Windows 2000 server with PHP 5.2.8.

    None of the solutions worked for me. PHP kept waiting for the response.

    Found the solution to be :

    $cmd = "E:\PHP_folder_path\php.exe E:\some_folder_path\backgroundProcess.php";
    pclose(popen("start /B ". $cmd, "a"));  // mode = "a" since I had some logs to edit
    

    ps : Posting the same reply to the other thread (PHP on a windows machine; Start process in background) since these 2 links helped me a lot in doing some research on this.

    0 讨论(0)
  • 2020-11-27 13:38

    From the manual : http://www.php.net/manual/en/function.exec.php

    Note:

    If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

    And a similar question I answered : Call another PHP script and return control to user before the other script completes

    0 讨论(0)
  • 2020-11-27 13:43

    It may well be that when using the exec() in a windows environment that redirection is to NUL:. /dev/null is a *nix null file.

    0 讨论(0)
  • 2020-11-27 13:48

    Problem solved with the following command:

    $WshShell = new COM("WScript.Shell");
    $oExec = $WshShell->Run("C:\wamp\bin\php\phpVERSIONNUMBER\php-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);
    
    0 讨论(0)
  • 2020-11-27 13:51

    In addition to Rohit's answer above, I edited his solution to work on Windows 10 PHP 7.0.3:

    pclose(popen("start /B ". $cmd, "w"));
    
    0 讨论(0)
提交回复
热议问题