/dev/null & echo \$!");...">

How to get PID from PHP exec () function on Windows?

I have always used:

$pid = exec("/usr/local/bin/php file.php $args > /dev/null & echo \$!"); 

But I use the XP virtual machine to develop a web application, and I have no idea how to get the pid in windows.

I tried this on cmd:

 C:\\wamp\\bin\\php\\php5.2.9-2\\php.exe "file.php args" > NUL & echo $! 

And it runs the file, but the output is "$!"

How can I get pid in var $ pid? (using php)

+7
windows php background exec background-process
source share
3 answers

You will need to install an additional extension but found a solution located in the Uniformserver Wiki .

UPDATE

After some searches, you can take a look at the tasklist , which coincided, you can use the PHP exec command to get what you need.

0
source share

I use Pstools , which allows you to create a process in the background and capture its pid:

 // use psexec to start in background, pipe stderr to stdout to capture pid exec("psexec -d $command 2>&1", $output); // capture pid on the 6th line preg_match('/ID (\d+)/', $output[5], $matches); $pid = $matches[1]; 

It's a bit hacky but it does its job

+8
source share

Here's a slightly less hacky version of SeanDowney's answer.

PsExec returns the PID of the spawned process as an integer exit code. So all you need is:

 <?php function spawn($script) { @exec('psexec -accepteula -d php.exe ' . $script . ' 2>&1', $output, $pid); return $pid; } // spawn echo spawn('phpinfo.php'); ?> 

The -accepteula argument is only needed the first time you start PsExec, but if you distribute your program, each user will run it for the first time, and nothing will prevent you from leaving it for each subsequent execution.,

PSTools is a quick and easy installation (just unzip PSTools somewhere and add its folder to your path), so there is no good reason not to use this method.

0
source share

All Articles