Reputation: 534
I am using python-daemon in my code that has print statements in it. I want to send them to a file so I ran the following:
python server.py >> log.out
However, nothing goes in log.out
.
Can anyone tell me what I need to do?
Thanks.
Upvotes: 5
Views: 4917
Reputation: 4860
The "problem" is that python-daemon closes all file descriptors, including stdout. You either need to do what @elarson suggested or (if you want to keep the stdout redirection when starting your daemon) exclude sys.stdout from being closed by adding something like
files_preserve = [sys.stdout]
to you DaemonContext invocation.
Upvotes: 0
Reputation: 865
The DaemonContext object allows redirecting stdout/stderr/stdin when you create the object. For example:
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)
You should be able to cat checking_print.log
and see the output from the print statements.
A good reference for the DaemonContext object is PEP 3143.
Upvotes: 6
Reputation: 4654
if you have an error in your code it will not be written to the file. See http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
Try creating this file:
print 'stdout'
raise Exception('stderr')
Upvotes: -1
Reputation: 11585
If it's already running as a daemon you'll most likely need to force redirection of STDOUT, STDERR etc. You can read more on I/O Redirection here.
python server.py 2>log.out >&2
Upvotes: -2