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.

2
  • I'm looking for PID of the process that is opened via popen. Commented Aug 7, 2011 at 13:32
  • You should add that as the first sentence of your question :) Commented Aug 7, 2011 at 13:51

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I've already read php.net/manual/en/function.proc-open.php#90584 but it is not kind of portable solution.
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 :)
sorry, my fault, haven't tested the code, just repaired snippet :)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.