0

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.

Community
  • 1
  • 1
MartyIX
  • 27,828
  • 29
  • 136
  • 207

1 Answers1

2

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);
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • I've already read http://www.php.net/manual/en/function.proc-open.php#90584 but it is not kind of portable solution. – MartyIX Aug 07 '11 at 13:40
  • You should use `proc_open` and `proc_close`. This snippet returns warning: `supplied resource is not a valid stream resource`. I'll add working code when I finish :) – MartyIX Aug 07 '11 at 14:22
  • sorry, my fault, haven't tested the code, just repaired snippet :) – Marek Sebera Aug 07 '11 at 14:26
  • A problem that bugs me is that I would like to get PID of $cmd but $pid now contains PID of `start` command (as expected) and I'm really at loss how to find out the subprocess PID. – MartyIX Aug 07 '11 at 16:38