【发布时间】:2019-10-02 14:03:57
【问题描述】:
我正在寻找一种方法来编写由 java 应用程序通过脚本执行的 python 运行日志。
假设我的脚本是:
import time
for x in range(120):
print("Running ", x)
time.sleep(1)
这是我目前的解决方案:
- 使用
java触发脚本
String cmd = "python script.py";
var process = Runtime.getRuntime().exec(cmd, null, new File(sandboxPath));
- 将日志写入新文件:
String traceLogCmd = String.format("strace -p %s -s 9999 -e trace=write -o output.txt", process.pid());
Runtime.getRuntime().exec(traceLogCmd, null, new File(sandboxPath));
现在的问题是 output.txt 只有在 python 脚本执行完毕时才有内容,所以我不能 tailf 输出文件。
同时,如果我直接从终端执行python script.py 和strace 命令,输出正是我所期望的。
如果我做错了什么或有其他方法获取 python 日志,有人可以纠正我吗?
提前致谢。
【问题讨论】: