Run multiple programs sequentially in one Windows command prompt?

I need to run multiple programs one after the other and they each run in a console window. I want the console window to be visible, but a new window is created for each program. This is annoying because each window is opened in a new position from where the other is closed and steals focus when working in Eclipse.

This is the initial code I was using:

def runCommand( self, cmd, instream=None, outstream=None, errstream=None ):
    proc = subprocess.Popen( cmd, stdin=instream, stdout=outstream, stderr=errstream )

    while True:
        retcode = proc.poll()
        if retcode == None:
            if mAbortBuild:
                proc.terminate()
                return False
            else:
                time.sleep(1)
        else:
            if retcode == 0:
                return True
            else:
                return False

I switched to opening a command prompt using 'cmd' when calling subprocess.Popen and then calling proc.stdin.write( b'program.exe\r\n' ). This seems to solve the one command window problem but now I can't tell when the first program is done and I can start the second. I want to stop and interrogate the log file from the first program before running the second program.

Any tips on how I can achieve this? Is there another option for running the programs in one window I haven't found yet?


Since you're using Windows, you could just create a batch file listing each program you want to run which will all execute in a single console window. Since it's a batch script you can do things like put conditional statements in it as shown in the example.

import os
import subprocess
import textwrap

# create a batch file with some commands in it
batch_filename = 'commands.bat'
with open(batch_filename, "wt") as batchfile:
    batchfile.write(textwrap.dedent("""
        python hello.py
        if errorlevel 1 (
            @echo non-zero exit code: %errorlevel% - terminating
            exit
        )
        time /t
        date /t
    """))

# execute the batch file as a separate process and echo its output
kwargs = dict(stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
              universal_newlines=True)
with subprocess.Popen(batch_filename, **kwargs).stdout as output:
    for line in output:
        print line,

try: os.remove(batch_filename)  # clean up
except os.error: pass