简体   繁体   English

在后台运行程序,并将其输出实时重定向到文件

[英]Run programs in background and redirect their outputs to file in real time

I want to run several python scripts simultaneously in one bash session and to inspect their outputs respectively in real time. 我想在一个bash会话中同时运行多个python脚本,并分别实时检查其输出。 To fulfil this task, I wrote a simple bash script which is shown below: 为了完成此任务,我编写了一个简单的bash脚本,如下所示:

#!/bin/bash
python 1.py > 1.output &
python 2.py > 2.output &
python 3.py > 3.output &

When I use cat 1.output command to check what have been printed in the middle of executing, however, nothing can be seen. 当我使用cat 1.output命令检查执行过程中已打印的内容时,什么都看不到。

After thinking a while, I realise that the 1.output must be filled when 1.py finishes executing. 想了一会儿后,我意识到1.output必须在填写1.py完成执行。 In other words, the method which I used here is not a real time fashion. 换句话说,我在这里使用的方法不是real time

You may propse that to wait for these python scripts' finish. 您可能会建议等待这些python脚本完成。 The fact, unfortunately, is that all there python scripts are actually long run programs, they maybe finish after days or months, and that is why I want to inspect their outputs in real time. 不幸的是,事实是那里所有的python脚本实际上都是长期运行的程序,它们可能在几天或几个月后完成,这就是为什么我要实时检查其输出。

Also, you may suggest me to modify the python scripts to print message to file directly instead of stdout . 另外,您可能建议我修改python脚本以将消息直接打印到文件而不是stdout Sorry, the scripts here are too complex to modify, there are chucks of print function in them. 抱歉,这里的脚本太复杂而无法修改,其中包含print功能。

what can I do now? 我现在能做什么?

The -u switch and the equivalent PYTHONUNBUFFERED environment variable forces stdout to be unbuffered. -u开关和等效的PYTHONUNBUFFERED环境变量强制将stdout取消缓冲。 Try this: 尝试这个:

#!/bin/bash
python -u 1.py > 1.output &
python -u 2.py > 2.output &
python -u 3.py > 3.output &

or 要么

#!/bin/bash
export PYTHONUNBUFFERED=yes
python 1.py > 1.output &
python 2.py > 2.output &
python 3.py > 3.output &

Note that -u has side effects: read the doc to learn more. 请注意, -u有副作用:请阅读文档以了解更多信息。

Reference: 参考:

If you can find some sort of main loop in the program, that could be a good indicator of progress, it would be good to do write-to-file. 如果您可以在程序中找到某种主循环,这可以很好地指示进度,则执行写入文件将是很好的。 If you say that the output does not fill until the output stream is closed by Python, then this is probably the simplest alternative. 如果您说在Python关闭输出流之前输出不会填满,那么这可能是最简单的选择。

You can try to override stderr/stdout in your scripts by doing: 您可以尝试通过执行以下操作来覆盖脚本中的stderr / stdout:

# in the beginning of your script
import os
import sys

desired_output_file = open('/path/to/file', 'a')
os.dup2(desired_output_file.fileno(), sys.stdout)
os.dup2(desired_output_file.fileno(), sys.stderr)

Then all print calls will write to this special file, not to stdout. 然后,所有print调用将写入此特殊文件,而不是stdout。 However, main problem could be with buffered IO. 但是,主要问题可能是缓冲的IO。 You have to somehow flush content of such file to the disk. 您必须以某种方式将此类文件的内容flush到磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM