redirecting sdtout/sdterr in a daemon
Carsten Gaebler
clpy at snakefarm.org
Tue Oct 30 11:20:12 EST 2001
Rajarshi Guha wrote:
> I'm using the daemonize.py module to make my code run as a daemon.
> However the module contains the lines:
>
> sys.stdout = NullDevice()
> sys.stderr = NullDevice()
>
> Instead of this behaviour I would like it to write output to a file. So I
> write:
>
> sys.stderr = open(outputhandle,'w')
> sys.stdout = open(outputhandle,'w')
>
> Where outputhandl is a passed to the function. However no output comes to
> the this file.
This is due to the lines that follow the STDERR/STDOUT
redirection:
> for z in range(3, 256):
> try: os.close(z)
> except: pass
This loop closes all file handles except for the "real"
STDIN/STDOUT/STDERR which are 0, 1 and 2. Especially your newly
opened files will be closed here. Open the files after the loop
and it should work as you expected.
cg.
More information about the Python-list
mailing list