7

What I'm trying to work out how to do is this: I've got a php file let's call trigger.php that runs some php code that sets off another php file we'll call backgroundProcess.php to start processing.

Although trigger.php needs to ignore what happens to backgroundProcess.php, it just has to start it processing and it will stop while backgroundProcess.php keeps going.

edit1

I'm running this on Windows Wampserver 2.1 So this has to be a windows command.

edit2

Solved it with the following command, thanks to jakenoble's suggestions:

exec("C:\wamp\bin\php\phpVERSION_NUMBER\php.exe -f C:\wamp\www\path\to\backgroundProcess.php");
hakre
  • 193,403
  • 52
  • 435
  • 836
Emmanuel
  • 4,933
  • 5
  • 46
  • 71

1 Answers1

7

You can use exec() and add an & to the end of the call, plus an output stream:

In trigger.php

exec("php backgroundProcess.php > /dev/null &");

You can find out more here http://php.net/manual/en/function.exec.php

Jake N
  • 10,535
  • 11
  • 66
  • 112
  • This doesn't work. I'm using WAMPSERVER 2.1 so perhaps that's the issue? Is there a way to work around this? – Emmanuel Mar 19 '11 at 11:10
  • Unless I'm doing something else wrong.... I've got both php files in the same folder and am running the the above command. – Emmanuel Mar 19 '11 at 11:11
  • 2
    It won't work, Windows probably doesn't know where `php` is installed and therefore cannot execute it. Try launching a command window and executing the script there, then take that code and use it in `exec()`. I don't use Windows so can't really help much more. You might want to revise your question as this changes everything. – Jake N Mar 19 '11 at 11:14
  • 1
    Solved! :) With the following command: `exec("C:\wamp\bin\php\phpVERSION_NUMBER\php.exe -f C:\wamp\www\path\to\backgroundProcess.php");` – Emmanuel Mar 19 '11 at 11:23
  • Unix or windows alike, this assumes the php location to be in the PATH variable (`%PATH%` for Windows and `$PATH` for unix). In unix it generally is, while in Windows it generally isn't. The alternative to adding the php directory to the path environment variable is inserting a full path. This is possible in windows and unix, but it won't be portable at all. – Jasper Jul 26 '13 at 13:54
  • This will not work on shared hosting accounts, such as GoDaddy. – Trass Vasston Jan 10 '17 at 00:49