Python shell scripting and errors

P

Phillip B Oldham

I've got a python script running as a daemon (using someone else's
daemon module). It runs fine for a while, but will occasionally balk
and die. Since its running in the background, I'm getting no error
from it.

What's the best way to capture the output into a file for later review?
 
T

Tim Wintle

I've got a python script running as a daemon (using someone else's
daemon module). It runs fine for a while, but will occasionally balk
and die. Since its running in the background, I'm getting no error
from it.

What's the best way to capture the output into a file for later review?

Point standard out to an open file handle?
 
P

Philip Semanchuk

Point standard out to an open file handle?

And stderr.

I don't know how the OP's daemon is being launched, but the crontab
command below worked for me for redirecting errors to a log file
without the need to modify the script.

python /usr/local/foo/bar.py >> /var/log/foo/bar.txt 2>&1



HTH
Philip
 
B

birdsong

And stderr.

I don't know how the OP's daemon is being launched, but the crontab  
command below worked for me for redirecting errors to a log file  
without the need to modify the script.

python /usr/local/foo/bar.py >> /var/log/foo/bar.txt 2>&1

If the daemon module is worth it's salt, it will have probably closed
all open filehandles including STDOUT and STDERR. So this might not
work.

If all you want is some temporary debugging, add some redirection for
stdout and stderr after you daemonize.

import sys
sys.stderr = open('/path/to/suitable_stderr_file', 'w')
sys.stdout = open('/path/to/suitable_stdout_file', 'w')


I'm pretty sure this should catch any tracebacks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top