closing stdin, stdout and stderr

Discussion in 'Python' started by Martijn Brouwer, Dec 26, 2005.

  1. 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
    Martijn Brouwer, Dec 26, 2005
    #1
    1. Advertising

  2. Martijn Brouwer

    Robin Becker Guest

    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/Cookbook/Python/Recipe/278731

    --
    Robin Becker
    Robin Becker, Dec 26, 2005
    #2
    1. Advertising

  3. Martijn Brouwer

    Robin Becker Guest

    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/Cookbook/Python/Recipe/278731
    >

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

    --
    Robin Becker
    Robin Becker, Dec 26, 2005
    #3
  4. 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/Cookbook/Python/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 <>
    Martijn Brouwer, Dec 27, 2005
    #4
  5. 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/Cookbook/Python/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
    Martijn Brouwer, Dec 27, 2005
    #5
  6. Martijn Brouwer

    Donn Cave Guest

    In article <>,
    Martijn Brouwer <> 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,
    Donn Cave, Dec 27, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Knop
    Replies:
    4
    Views:
    1,013
    Jan knob
    Nov 24, 2003
  2. Vincent Touquet
    Replies:
    1
    Views:
    573
    Adrian B.
    Sep 3, 2004
  3. Vincent  Touquet
    Replies:
    0
    Views:
    433
    Vincent Touquet
    Sep 6, 2004
  4. Michael McGarry
    Replies:
    1
    Views:
    600
    Michael Fuhr
    Dec 16, 2004
  5. , India

    initialization of stdin, stdout, stderr

    , India, Oct 27, 2008, in forum: C Programming
    Replies:
    5
    Views:
    819
    Phil Carmody
    Oct 28, 2008
Loading...

Share This Page