Python shell scripting and errors

birdsong david.birdsong at gmail.com
Mon Feb 23 14:49:03 EST 2009


On Feb 23, 9:34 am, Philip Semanchuk <phi... at semanchuk.com> wrote:
> On Feb 23, 2009, at 12:21 PM, Tim Wintle wrote:
>
> > On Mon, 2009-02-23 at 09:12 -0800, Phillip B Oldham wrote:
> >> 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?
>
> 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.

>
> HTH
> Philip




More information about the Python-list mailing list