http://www.perlmonks.org?node_id=985748


in reply to How to close command prompts opened with perl script after execution that script

Starting the cmd windows as you are, it is difficult to kill them as you don't have a handle or PID for them.

If your commands are unique enough, you might use taskkill to kill the processes.

You could use Win32::Process to start the command processes then terminate them, something like:

use strict; use warnings; use Win32::Process; my $cmd = 'dir'; #system("start cmd.exe /k $cmd"); my $processObj; Win32::Process::Create( $processObj, "C:/windows/system32/cmd.exe", "cmd.exe /k $cmd", 0, NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE, "." ); sleep(10); # do other stuff $processObj->Kill(0);