spranav has asked for the wisdom of the Perl Monks concerning the following question:

I want to run a terminal emulator from perl script in a while/for loop on Windows XP. Following code does not wait for the first terminal emulator to close, it opens up the terminal emulator two times. Is there any way perl can wait until first terminal has exited?
$MY_VT = "c:/test/terminal.exe"; $i = 2; while ($i) { print "Execute $i\n"; system ($MY_VT)== 0 or die "issues running my_vt"; wait(); $i--; }
Thanks,
Pranav

20100409 Janitored by Corion: Removed <font> tag

Replies are listed 'Best First'.
Re: system() does not wait for terminal.exe to complete executing on Windows
by BrowserUk (Patriarch) on Apr 07, 2010 at 21:27 UTC

    There is something wrong with the logic of your script. The wait is useless here and entirely unnecessary.

    As coded, system should not return until the program started completes. I cannot think of any circumstance where that would not be the case.

    For example, if you run this:

    perl -le"$|++; for( 1, 2){ print system qq[C:/windows/system32/notepad +.exe]; print'hi'}"

    You will see that you have to close notepad.exe before control returns to the perl script. It then prints 'hi' and the return value from the command. You have to close notepad twice before the above script completes.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks. Your code snippet helped me :-). Here is what I observed:- I was trying to run a short-cut(to an terminal.exe) with command line parameter in my original code. When I tried your code it behaved the same way. So I tried running the terminal.exe from its install location, and it worked :-). The script executes terminal.exe, waits for it to exit and then runs it second time. Thanks !!!
        Here is what I observed:- I was trying to run a short-cut(to an terminal.exe) with command line parameter in my original code.

        Ah! Yes, shortcuts effectively detach their targets and then return. Which explains the behaviour you got.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: system() does not wait for terminal.exe to complete executing on Windows
by ww (Archbishop) on Apr 07, 2010 at 19:14 UTC
    Please remove your <font size="10" color="blue"> tags. They are
    Unnecessary
    Unwanted

    Then, if you can, please show us some relationship between the code you've posted and the problem you assert. Lacking a box with XP running, I'm also more than a bit confused about:

    1. What you're trying to do; what you want to accomplish?
    2. Why achieving that requires opening terminal.exe more than once?
    3. ...and, if so, why do it from a loop?
    4. and perhaps, what is "terminal.exe" that it's found in the C:/test/ directory?

    Modified to use strict and warnings and to run under Linux, the script does exactly what one might expect after reading the documentation for system. Modified by substituting another (available) executable for terminal.exe the script (under w2k) also behaves as expected... and not as described.

    So you might want to insert a little test right after your wait();: print "\n\t Past the wait; first execution of terminal.exe is complete\n\n"

    And see what happens.

      Thanks for your response, let me try to explain:- The terminal.exe is Terminal Emulator (like hyper term); it logs in to a hardware device over serial port and runs some command, when it is done it closes itself. Problem:- The script opens up two terminals I want it to open a terminal in first pass, wait until the terminal ends, then open the terminal again in second pass.