bash 在后台运行程序并将其输出实时重定向到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31345861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run programs in background and redirect their outputs to file in real time
提问by Douglas Su
I want to run several python scripts simultaneously in one bash session and to inspect their outputs respectively in real time. To fulfil this task, I wrote a simple bash script which is shown below:
我想在一个 bash 会话中同时运行多个 python 脚本并分别实时检查它们的输出。为了完成这个任务,我编写了一个简单的 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. In other words, the method which I used here is not a real time
fashion.
想了想,发现执行完1.output
一定要填1.py
。换句话说,我在这里使用的方法不是一种real time
时尚。
You may propse that to wait for these python scripts' finish. 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 脚本完成。不幸的是,事实上所有 Python 脚本实际上都是长时间运行的程序,它们可能会在几天或几个月后完成,这就是我想实时检查它们的输出的原因。
Also, you may suggest me to modify the python scripts to print message to file directly instead of stdout
. Sorry, the scripts here are too complex to modify, there are chucks of print
function in them.
此外,您可能会建议我修改 python 脚本以将消息直接打印到文件而不是stdout
. 抱歉,这里的脚本太复杂了,无法修改,里面有很多print
功能。
what can I do now?
我现在能做什么?
回答by Rob?
The -u
switch and the equivalent PYTHONUNBUFFERED
environment variable forces stdout to be unbuffered. Try this:
该-u
开关和等效PYTHONUNBUFFERED
环境变量的力量标准输出是缓冲。尝试这个:
#!/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:
参考:
回答by bcdan
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 关闭之前输出不会被填充,那么这可能是最简单的选择。
回答by Ivan Velichko
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. However, main problem could be with buffered IO. You have to somehow flush
content of such file to the disk.
然后所有print
调用都将写入这个特殊文件,而不是标准输出。但是,主要问题可能出在缓冲 IO 上。您必须以某种方式flush
将此类文件的内容写入磁盘。