Preserving bash redirection in a python subprocess

后端 未结 3 752
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 02:12

To begin with, I am only allowed to use python 2.4.4

I need to write a process controller in python which launches and various subprocesses monitors how they affect the

3条回答
  •  [愿得一人]
    2021-01-29 02:42

    If I understand your question correctly, it sounds like what you are looking for here is to be able to launch a list of scripts with the output redirected to files. In that case, launch each of your tasks something like this:

    task = subprocess.Popen(['python', 'myscript', 'arg1', 'arg2', 'arg3'],
        stdout=open('output.log', 'w'), stderr=open('err.log', 'w'))
    

    Doing this means that the subprocess's stdout and stderr are redirected to files that the monitoring process opened, but the monitoring process does not have to be involved in copying data around. You can also redirect the subprocess stdins as well, if needed.

    Note that you'll likely want to handle error cases and such, which aren't handled in this example.

提交回复
热议问题