我正在通过iTerm在bash控制台上运行此命令
{ cd /usr/local/path/to/code; echo "hi1"; sudo chmod 777 /tmp/dissolve.log; echo "hi2"; python someapp/runner.py dissolve; echo "hi3"; } > /tmp/dissolve.log &
在尾随文件我得到:
tail: /tmp/dissolve.log: file truncated
hi1
hi2
我无法弄清楚为什么我没有得到文件
python someapp/runner.py dissolve
的输出,当我执行cmd + c时,预期的输出出现在tail
日志中。runner.py
中的代码段:if __name__ == '__main__':
program_name = sys.argv[1]
if program_name == 'dissolve':
obj = SomeClass() # this is properly imported
obj.some_function() # this has lot of `print` statements, which i intened to catch in '/tmp/dissolve.log'
print
中的初始some_function()
是否在/tmp/dissolve.log
以外的其他地方传递值?有什么建议为什么会这样吗?
最佳答案
在您将输出发送到文件时,这似乎是一个buffering问题。您可以使用stdbuf
强制行缓冲,如下所示:
{ cd /usr/local/path/to/code;
echo "hi1";
sudo chmod 777 /tmp/dissolve.log;
echo "hi2";
stdbuf -oL python someapp/runner.py dissolve;
echo "hi3"; } > /tmp/dissolve.log &