Question

I am running Windows 7 64bit for WAMP 2 server.

I am running my program from batch script using Windows Com Component for ex:

C:\wamp\bin\php\php5.3.13\php.exe C:\wamp\www\test\command.php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($command, 7, false);

Now When I find how many "cmd.exe" programs are running, it list me all the processes using below command:

tasklist /svc /fi "imagename eq cmd.exe"

And then I kill them with below command using php script:

$output = shell_exec('taskkill /F /IM "cmd.exe"');

Here, what happens is, not all my cmd.exe windows getting closed.

What might be error in above code? Some windows are closed, while some remains open which is executing the command.

Please help.

Was it helpful?

Solution

Found a solution [though open for better suggestions]

First needs to check and kill if php tasks exists, then command prompt will kill :

// It will first list out all `php.exe` tasks running
$output = shell_exec('tasklist /svc /fi "imagename eq php.exe"');
print_r($output);
echo "<br> ------------------------------------ <br>";

// It will first list out all `cmd.exe` tasks running
$output = shell_exec('tasklist /svc /fi "imagename eq cmd.exe"');
print_r($output);
echo "<br> ------------------------------------ <br>";

// kills php.exe tasks
$php_output = shell_exec('taskkill /F /IM "php.exe"');
print_r($output);
echo "<br> ------------------------------------ <br>";

// kills cmd.exe tasks
$cmd_output = shell_exec('taskkill /F /IM "cmd.exe"');
print_r($output);
echo "<br> ------------------------------------ <br>";

die(ucfirst('all tasks killed'));

Hope it helps to all !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top