Page 1 of 1

Need help w/ Creating and running background process

Posted: Wed Apr 14, 2010 2:30 pm
by poly4life
Hello,

I have been on php.net looking up popen, exec, the whole nine-yards and I still cannot figure out how to run a process in the background. Let me give a little summary of the events.

The user submits data on the web page, which then hands it off to the web server and talks to the php server and the sql server I'm running. Now, from the php engine, I make a soapclient call to a soapserver, which resides in a different machine not connected to the internet but to an intranet and has it's own php engine. From the intranet server, I have some php code that is supposed to create a background process, which will execute a batch file/ The problem is php is hanging until the process is finished and this is not the desired result. What I wish to do is have this background process running and return back to the web server and to the user on the client side.

Here is the little snippet of code to make the process call from the intranet server:

function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
}

function Results_BeforeShow(& $sender)
{

$server = new SoapServer(null,array("uri"=>"urn:ServNamespace"));
$server->addFunction(array("CallF"));
$server->handle();

$Results_BeforeShow = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $Results; //Compatibility
//End Results_BeforeShow

return $Results_BeforeShow;
}

function CallF()
{
// code
...
// more code
...
execInBackground("d:\\Batch.bat");

}

This is the contents of the batch file:
cd d:\
c:\php\php.exe phpfile.php > log.txt
exit


When Results_BeforeShow is finished, the call to the intranet server is finished and it will be returned back to the web server, via the soapcall, which will then return back to the user.


I would like to get php to stop hanging first. What I'm trying to do is create a process that will run a batch file and allow control to be returned to the user. So, it's not just run a process in the background and do other php code. No, run the process in the background, as if I started it up from the local machine itself and have it not tied in anyway to something that is expecting a response back, like a web server. It seems like the intranet server is waiting for the batch file to finish. And I think part of the problem is exec(), popen(), and the like are creating child processes, I believe, which depend on the process that called it. So, if I want to run a batch file and then exit the main php program (as opposed to the external php program which calls the batch file), it'll throw its hands up. I need to a create a process that is entirely independent of other processes, as well as the intranet server, so as to not expect a return response.

I hope that makes sense. Any help would be appreciated. Thank you for your time.