python-daemon不记录stdout重定向

Cra*_*ete 4 python daemon stdout

我在我的代码中使用python-daemon,其中包含print语句.我想将它们发送到一个文件,所以我运行了以下内容:

python server.py >> log.out
Run Code Online (Sandbox Code Playgroud)

然而,没有任何进展log.out.

谁能告诉我我需要做什么?

谢谢.

ela*_*son 5

DaemonContext对象允许在创建对象时重定向stdout/stderr/stdin.例如:

import os
import daemon


if __name__ == '__main__':
    here = os.path.dirname(os.path.abspath(__file__))
    out = open('checking_print.log', 'w+')

    with daemon.DaemonContext(working_directory=here, stdout=out):
        for i in range(1, 1000):
            print('Counting ... %s' % i)
Run Code Online (Sandbox Code Playgroud)

您应该能够cat checking_print.log查看print语句的输出.

对DaemonContext对象的一个​​很好的参考是PEP 3143.


Chr*_*tts -2

如果它已经作为守护进程运行,您很可能需要强制重定向 STDOUT、STDERR 等。您可以在此处阅读有关 I/O 重定向的更多信息。

python server.py 2>log.out >&2
Run Code Online (Sandbox Code Playgroud)