MartyIX
MartyIX

Reputation: 28648

Executing a background process in PHP on Windows

I'm rewriting Mark's answer for Windows and so far I've come up with this:

 // Escape character for Windows is: ^ 
 $shellCmd = 'start /B cmd /c ' . escapeshellcmd($cmd) . ' ^>"'.$outputfile. '"'; 
 // note that exec was like 40 times slower than popen & pclose 
 pclose(popen($shellCmd, "r")); 

There's tasklist command on Windows but I don't know how to find out the PID of my process. To be punctual I'm looking for PID of the process that is opened via popen.

Can you help me? Thanks!

Note: I'm not sure what this code does with error output but in my case it doesn't matter.

Upvotes: 0

Views: 1326

Answers (1)

Marek Sebera
Marek Sebera

Reputation: 40621

http://php.net/manual/en/function.proc-open.php
http://php.net/manual/en/function.pcntl-fork.php
http://www.php.net/proc_get_status

read discussion under these functions and you can get more control over background processes and retrieve their PIDs

example like this:

$pcs = popen($shellCmd,"r");
$info = proc_get_status($pcs);
$pid = $info['pid'];
proc_close($pcs);

Upvotes: 2

Related Questions