Welcome, guest | Sign In | My Account | Store | Cart

When creating daemons on Unix-like systems, it's typical to close or redirect stdin, stdout, and stderr. This simple recipe demonstrates that it's not quite as obvious as it might first appear.

Python, 9 lines
1
2
3
4
5
6
7
8
9
# here's the obvious part!
sys.stdin.close()
sys.stdout.close()
sys.stderr.close()

# this is pretty obscure!
os.close(0)
os.close(1)
os.close(2)

When would you want to use this? If you have a daemon process and really want to close or redirect the standard file descriptors, you must close or redirect at both the Python and C levels. Consider starting a daemon while ssh'd into a server:

% (mydaemon.py When would you want to use this?  If you have a daemon process and really

want to close or redirect the standard file descriptors, you must close or redirect at both the Python and C levels. Consider starting a daemon while ssh'd into a server:

% (mydaemon.py

1 comment

Mike Brown 21 years ago  # | flag

? Uh, where's the rest of the explanation?

Created by skip on Sat, 1 Mar 2003 (PSF)
Python recipes (4591)
skip's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks