26

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 for example I have trigger.php that runs the exec function that calls for my backgroundProcess.php to be parsed and executed.

However the problem is that my trigger.php file is waiting for the exec() command to finish running backgroundProcess.php before it stops. The background process runs for about 20-30 seconds, and trigger.php is waiting all that time until backgroundProcess.php has fully finished.

Is that making sense? Here is the trigger.php file that runs the exec() command

exec('C:\wamp\bin\php\php'.phpversion().'\php.exe -f C:\path\to\backgroundProcess.php > C:\wamp\bin\php\php'.phpversion().'\dev\null &');

Basically, I'm wanting trigger.php to just trigger off the backgroundProcess and not wait around for it to finish.

Blue
  • 22,608
  • 7
  • 62
  • 92
Emmanuel
  • 4,933
  • 5
  • 46
  • 71

6 Answers6

26

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);
Emmanuel
  • 4,933
  • 5
  • 46
  • 71
  • This have been causing problems to me in a random manner. Throwing exceptions about not being able to run the command. Check my answer. – Muhammad Gelbana Sep 02 '13 at 11:16
  • 1
    @Muhammad Gelbana You dont have an answer in this question.. Care to be a bit more specific or link to the answer you are referring to? – computrius Feb 13 '14 at 20:23
  • 2
    It was deleted ! Any way, here is a summary of what I wrote `exec("CMD /C start yourcommand here"); I think the /C parameter switch isn't necessary, but I'm just being extra sure. Prefix your command with dirname(__FILE__) if you want to execute in the current directory of the script. This can only work if the start command exists on your windows machine.` – Muhammad Gelbana Feb 13 '14 at 23:11
10

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.

Community
  • 1
  • 1
Rohit
  • 439
  • 8
  • 18
4

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

Community
  • 1
  • 1
JohnP
  • 49,507
  • 13
  • 108
  • 140
  • @JohnP - Yes, but I thought I was doing that with the `> C:\wamp\bin\php\php'.phpversion().'\dev\null &` bit at the end? – Emmanuel Mar 20 '11 at 08:27
  • I think you need to create a file descriptor and pass it in as the second parameter. Not just pipe it. – JohnP Mar 20 '11 at 08:39
  • @JohnP - I haven't heard about a file descriptor before... At least not that term. Could you give a quick example? – Emmanuel Mar 20 '11 at 08:41
  • it's just a pointer to a file. `$fd = fopen('dump.txt', 'a')`. Just a fancy term for fd :) – JohnP Mar 20 '11 at 08:49
  • @JohnP - Nope, adding a file descriptor didn't do anything. But worked out something else, so Problem Solved!!! See above **edit** :) – Emmanuel Mar 20 '11 at 09:27
  • @Emmanuel, oh? hmm, maybe it's a windows thing. COM will only work on windows, I think. Also, I think you should post that as an answer and mark it as accepted if it worked for you. People with the same issue could use that :) – JohnP Mar 20 '11 at 09:37
3

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.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
  • 1
    It's not something that can be queued. It's an ssh connection to a remote server and processing some things. – Emmanuel Mar 20 '11 at 07:34
2

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"));
1

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.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
John
  • 81
  • 1
  • 1