python守护进程未记录stdout重定向
我在代码中使用python守护进程,其中包含print语句。我想将它们发送到一个文件,因此我运行了以下操作:python守护进程未记录stdout重定向,python,stdout,daemon,Python,Stdout,Daemon,我在代码中使用python守护进程,其中包含print语句。我想将它们发送到一个文件,因此我运行了以下操作: python server.py >> log.out 但是,没有任何内容进入log.out 有人能告诉我需要做什么吗 谢谢。如果它已经作为守护进程运行,您很可能需要强制重定向STDOUT、STDERR等。您可以阅读更多 如果代码中有错误,它将不会写入文件。看 尝试创建此文件: print 'stdout' raise Exception('stderr') Daemon
python server.py >> log.out
但是,没有任何内容进入log.out
有人能告诉我需要做什么吗
谢谢。如果它已经作为守护进程运行,您很可能需要强制重定向STDOUT、STDERR等。您可以阅读更多
如果代码中有错误,它将不会写入文件。看 尝试创建此文件:
print 'stdout'
raise Exception('stderr')
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)
您应该能够cat checking\u print.log
并查看print语句的输出
DaemonContext对象的一个很好的参考是 你能给我们看一些示例代码吗?
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)