472,110 Members | 2,231 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

closing stdin, stdout and stderr

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

Dec 26 '05 #1
5 3416
Martijn Brouwer wrote:
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/Coo.../Recipe/278731

--
Robin Becker
Dec 26 '05 #2
Robin Becker wrote:
Martijn Brouwer wrote:
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/Coo.../Recipe/278731

but perhaps you're using a non unix OS, which will make that recipe wrong.

--
Robin Becker
Dec 26 '05 #3
On Mon, 2005-12-26 at 23:13 +0000, Robin Becker wrote:
Martijn Brouwer wrote:
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/Coo.../Recipe/278731


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().
Martijn

--
Martijn Brouwer <e.***********@alumnus.utwente.nl>

Dec 27 '05 #4
On Mon, 2005-12-26 at 23:15 +0000, Robin Becker wrote:
Robin Becker wrote:
Martijn Brouwer wrote:
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/Coo.../Recipe/278731

but perhaps you're using a non unix OS, which will make that recipe wrong.


Well, I am writing a unix daemon ;)

Martijn
Dec 27 '05 #5
In article <ma***************************************@python. org>,
Martijn Brouwer <e.***********@alumnus.utwente.nl> wrote:
....
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, do**@u.washington.edu
Dec 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jan Knop | last post: by
reply views Thread by lickspittle | last post: by
6 posts views Thread by ccdrbrg | last post: by
13 posts views Thread by Vincent Delporte | last post: by
reply views Thread by Gabriel Genellina | last post: by
8 posts views Thread by subramanian100in | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.