Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have always used:

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

But I am using an XP virtual machine to develop a web app and I have no idea how to get the pid in windows.

I tried this on a cmd:

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

And it gets the file executed, but the output is "$!"

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

like image 394
jarkam Avatar asked Sep 09 '10 18:09

jarkam


1 Answers

I'm using Pstools which allows you to create a process in the background and capture it's 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 little hacky, but it gets the job done

like image 78
SeanDowney Avatar answered Sep 24 '22 02:09

SeanDowney