2

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
qinHaiXiang
  • 6,051
  • 12
  • 47
  • 61
  • You can find out about PIDs under windows using the `tasklist` command. I can not provide a demo because taskkill and tasklist are broken on my computer and I have no clue how to fix them. – hakre Aug 04 '12 at 09:53
  • This question might be helpful: http://stackoverflow.com/questions/9348572/execute-bat-file-without-waiting-for-end/9348938#9348938 – dev-null-dweller Aug 04 '12 at 09:55

2 Answers2

2

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

Zbigniew
  • 36
  • 1
0

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

exec($command, $output);
$pid = (int)$output[0];
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • I am running your code and get any PID result,it just lunch the cmd.exe and program.exe [the external program I used].... – qinHaiXiang Aug 04 '12 at 10:09
  • @qinHaiXiang If you launch `cmd.exe` and use it to launch `program.exe`, then of course you will get the PID from `cmd.exe`, which is pretty useless. Why don't you launch `program.exe` directly? – Tomalak Aug 04 '12 at 10:19
  • I am using fastcopy[ the command line mode ] to copy files in background.My command is:"'fastcopy.exe .....';",the exec() auto launch the cmd.exe and then fastcopy.exe..... – qinHaiXiang Aug 04 '12 at 11:49