在新贵工作中从 python 捕获标准输出
当我将 python2.6 脚本作为新贵作业运行时,我正在努力将其 stdout 输出保存到文件中。
这是我当前的测试脚本:
#!/usr/bin/python
from time import sleep
import sys
while 1:
print "Hello from stdout"
print >> sys.stderr, "Hello from stderr"
sleep(1)
如果我执行 $ myscript >>> /var/log/myscript
工作正常。我都看到了。
但是,作为一个新贵脚本,如果我使用以下conf
/etc/init/myscript.conf:
exec /path/to/myscript >>> /var/log/myscript 2>&1
我看到这个:
来自 stderr 的问候 来自标准错误的你好 来自标准错误的你好 来自标准错误的你好 来自标准错误的你好 来自标准错误的你好
如果我对 stderr 和 stdout 使用不同的文件,它们都会被创建,但 stdout 始终为空。
我做错了什么?
I'm struggling to save the stdout output of a python2.6 script into a file when I run it as an upstart job.
This is my current test script:
#!/usr/bin/python
from time import sleep
import sys
while 1:
print "Hello from stdout"
print >> sys.stderr, "Hello from stderr"
sleep(1)
if I do $ myscript >> /var/log/myscript
that works fine. I see both.
However, as an upstart script, if I use the following conf
/etc/init/myscript.conf:
exec /path/to/myscript >> /var/log/myscript 2>&1
I see this:
Hello from stderr Hello from stderr Hello from stderr Hello from stderr Hello from stderr Hello from stderr
If I use a different file for stderr and stdout, they are both created, but the stdout is always empty.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓冲和刷新是猜测。
stderr 通常是无缓冲的流,而 stdout 是缓冲的。当您将其发送到文件时,它将以 4k 块的形式进行缓冲(通常)。
如果您进行以下更改:
您应该在文件中看到输出。
Buffering and flushing would be the guess.
stderr is typically an unbuffered stream while stdout is buffered. When you send it to a file it will be buffered in 4k chunks (typically).
If you make the following change:
You should see output in your file.