我在我的代码中使用了python-daemon,其中包含print语句。我想把它们发送到一个文件中,所以我运行了以下命令:
python server.py >> log.out然而,在log.out中什么都做不到。
有人能告诉我我需要做什么吗?
谢谢。
发布于 2013-10-18 14:20:48
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_print.log语句的输出。
PEP 3143是DaemonContext对象的一个很好的参考。
发布于 2012-06-22 04:31:14
如果您的代码中有错误,则不会将其写入文件。请参阅http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
尝试创建此文件:
print 'stdout'
raise Exception('stderr')发布于 2012-06-22 04:30:31
如果它已经作为守护进程运行,你很可能需要强制重定向STDOUT,STDERR等。你可以在I/O Redirection here上阅读更多。
python server.py 2>log.out >&2https://stackoverflow.com/questions/11146128
复制相似问题