Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by snreddy_gopu (Novice)
on Aug 06, 2012 at 12:48 UTC ( #985696=perlquestion: print w/ replies, xml ) Need Help??
snreddy_gopu has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl script which opens 3 command prompts during execution. Script need those command prompts upto to the completion of that script. But after completion of execution of script, the prompts are still opened. How can i close those prompts at the end of the execution of script. I have used following script to open new command prompts using perl.
$cmd="command1"; system("start cmd.exe /k $cmd");
thanks, gsnreddy

Comment on How to close command prompts opened with perl script after execution that script
Download Code
Re: How to close command prompts opened with perl script after execution that script
by aitap (Deacon) on Aug 06, 2012 at 13:19 UTC
    I may have misunderstood something, but what if you use cmd /c instead of cmd /k?
    Read perlipc if you need something more complex.
    Sorry if my advice was wrong.
Re: How to close command prompts opened with perl script after execution that script
by ww (Bishop) on Aug 06, 2012 at 13:35 UTC
    "RTFM" doesn't apply only to Perl:
    C:\>cmd /? Starts a new instance of the Windows command interpreter CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V: +OFF] [[/S] [/C | /K] string] /C Carries out the command specified by string and then terminate +s /K Carries out the command specified by string but remains
Re: How to close command prompts opened with perl script after execution that script
by ig (Vicar) on Aug 06, 2012 at 15:09 UTC

    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);
      Thanks ig. It's working fine. I have small doubt. I have opened two command prompts as you mentioned above using perl script. Is it possible to run any commands which are not specified in $cmd variable on opened prompts? I mean to say "First perl script opens a command prompt. Then executes whatever the command in the $cmd varible. Now i want to run one more command on that opened prompt before closing it using $processObj->Kill(0)"

        That I don't know.

        Several options come to mind:

        • Implement the required functions in Perl so I don't have to call a bunch of other executables
        • Run each command separately (system, backticks or whatever), reading output back into perl, then present the output as appropriate, rather than trying to manage a bunch of separate command windows and processes running cmd.exe
        • Compose a batch file then run it
        • Try to establish a pipe to STDIN of cmd.exe and feed it commands, but then probably need something like IPC3::Open and Expect (but maybe not Expect itself on Windows - I don't know if it works there) and it quickly gets more complicated.

        Having a *nix background, I don't much like the Windows way of doing process management. I find they make simple things, like process management and IPC very hard to do. For example, I still haven't found a simple way to get unbuffered I/O between a parent process and its child, or do anything like what process groups make easy on *nix. Being lazy, I do my best to avoid such issues. But this is my fault - probably not something you should emulate.

        $processObj->Kill(0); is not working for me to close the created process object, can anyone help me on this. thanks, Sarath S

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985696]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-07-23 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls