简体   繁体   中英

How to get process PID from PHP on Windows

I am developing a web-based application and I need to run a Matlab script to process some information.

The problem is that I have a limitation on the maximum number of Matlab processes running at the same time and because of this, I have to get the PID of each process in order to know if any of them has crashed and which one it was.

I have used some methods to get its PID, but for some reason running a simple command like 'notepad.exe' works fine and gets the correct PID, but when I run my script it gets the wrong PID.

One of the methods I tried is this one:

$process = "matlab";
$command = "-sd ".$softExecPath." -r \"analyse('".$videoDataPath."', '".$id_video_data."') \" ";
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->exec($process." ".$command);
$pid = intval($oExec->ProcessID);

And the other one is:

$process = "matlab";
$command = "-sd ".$softExecPath." -r \"analyse('".$videoDataPath."', '".$id_video_data."') \" ";
$command = $process." ".$command;
// use psexec to start in background, pipe stderr to stdout to capture pid
exec("C:/AppServ/www/PsTools/psexec.exe -d -accepteula $command 2>&1", $output);
// capture pid on the 6th line
preg_match('/ID (\d+)/', $output[5], $matches);
$pid = $matches[1];

Both of them get a PID but none of them is the real one.

On Windows, the Matlab program that you call to launch Matlab (the one pointed to by the shortcut in the Start Menu and on the path if you added it) is just a launcher program that goes on to run another matlab.exe which is the actual program, and then returns immediately. The PID you're getting back is that of the launcher program, which'll be gone by the time the real Matlab is doing work.

Two things you can do: You can run the actual matlab.exe program directly by finding it in the bin/win32 or similar platform-specific directory under the Matlab installation. Then you'll get the right PID back. Or you can fiddle with the -wait option to make the launcher program run synchronously, which means it'll block until the child matlab.exe exits, so it'll still show up in the process list while Matlab is running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM