closing stdin, stdout and stderr

M

Martijn Brouwer

I am writing a unix daemon in python, so I want to close stdin, stdout
and stderr.
My first attempt was to the standard file descriptors using their
close() methods. After closing stdout, I could not print anymore, so
this seemed to work. However, later I noticed that they were not really
closed. When I close them using os.close(), it did work.
What is the difference between these two methods and what is the reason
behind it? It took me a day to find out why I could not log out after
starting the daemon.

Martijn
 
R

Robin Becker

Martijn said:
I am writing a unix daemon in python, so I want to close stdin, stdout
and stderr.
My first attempt was to the standard file descriptors using their
close() methods. After closing stdout, I could not print anymore, so
this seemed to work. However, later I noticed that they were not really
closed. When I close them using os.close(), it did work.
What is the difference between these two methods and what is the reason
behind it? It took me a day to find out why I could not log out after
starting the daemon.

Martijn
I've had excellent results with variants of the cookbook entry at

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
 
D

Donn Cave

Martijn Brouwer said:
I read this one, which was the reason that I tried os.close instead of
sys.stdXXX.close(). But I would like to know why it does not close a
file discriptor is I call its close method().

They're special. I suppose because of internal dependencies - last
chance exception handler etc. - they are created without a close
function, internally, so close() has no effect. I don't know if it
really makes any sense, since anyway one may close the file descriptors
directly as you did.

Donn Cave, (e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top