在一个Windows命令提示符下顺序运行多个程序? [英] Run multiple programs sequentially in one Windows command prompt?

查看:139
本文介绍了在一个Windows命令提示符下顺序运行多个程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个接一个地运行多个程序,并且它们都在一个控制台窗口中运行。
我希望控制台窗口可见,但为每个程序创建一个新窗口。这是令人讨厌的,因为每个窗口在另一个关闭的新位置打开,并在Eclipse中工作时窃取焦点。

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

当调用subprocess.Popen然后调用proc.stdin.write(b'program.exe \\\\'')时,我切换到使用'cmd'打开命令提示符。
这似乎解决了一个命令窗口的问题,但现在我无法知道第一个程序何时完成,我可以开始第二个。在运行第二个程序之前,我想停止并询问第一个程序的日志文件。

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?

推荐答案

由于您使用的是Windows,您可以创建一个批处理文件,列出您要运行的每个程序,这些程序都将在单个控制台窗口中执行。因为它是一个批处理脚本,你可以做一些事情,如放置条件语句,如示例所示。

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

这篇关于在一个Windows命令提示符下顺序运行多个程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆