qinHaiXiang
qinHaiXiang

Reputation: 6419

Is it possible to get the PID of a process launched by exec() in Windows XP?

I am using the exec() function to execute the same external programs, and I want to stop one of them in some case. But the following command:

taskkill /IM program.exe

will get all program.exe killed. So the best way I thought was to kill program process by its PID.

So,I thought the way was to get the PID every time the program was executed, and then kill it.

I am using PHP 5.3 on Windows XP.

Upvotes: 2

Views: 4121

Answers (2)

Zbigniew
Zbigniew

Reputation: 36

exec on Windows hangs until child process is over. You need PID of a child, so I suppose you want to nohup a child.

Try this code, it worked for me. It nohups notepad.exe and displays its PID

    $command = 'notepad.exe';
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->exec("notepad.exe");
print_r ( $oExec->ProcessID ) 

pay attention to $WshShell->exec and not $WshShell->run as some googled ressources claim.

May it help someone else

Upvotes: 2

Tomalak
Tomalak

Reputation: 338228

The PID should be returned as the first element of the $output array.

exec($command, $output);
$pid = (int)$output[0];

Upvotes: 0

Related Questions