Mercurial > genshi > mirror
comparison examples/trac/trac/util/daemon.py @ 39:93b4dcbafd7b trunk
Copy Trac to main branch.
author | cmlenz |
---|---|
date | Mon, 03 Jul 2006 18:53:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
38:ee669cb9cccc | 39:93b4dcbafd7b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # | |
3 # Copyright (C) 2006 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://trac.edgewall.com/license.html. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://projects.edgewall.com/trac/. | |
13 | |
14 import os | |
15 import sys | |
16 | |
17 def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): | |
18 """Fork a daemon process (taken from the Python Cookbook).""" | |
19 | |
20 # Perform first fork | |
21 pid = os.fork() | |
22 if pid > 0: | |
23 sys.exit(0) # exit first parent | |
24 | |
25 # Decouple from parent environment | |
26 os.chdir('/') | |
27 os.umask(0) | |
28 os.setsid() | |
29 | |
30 # Perform second fork | |
31 pid = os.fork() | |
32 if pid > 0: | |
33 sys.exit(0) # exit first parent | |
34 | |
35 # The process is now daemonized, redirect standard file descriptors | |
36 for fileobj in sys.stdout, sys.stderr: | |
37 fileobj.flush() | |
38 stdin = file(stdin, 'r') | |
39 stdout = file(stdout, 'a+') | |
40 stderr = file(stderr, 'a+', 0) | |
41 os.dup2(stdin.fileno(), sys.stdin.fileno()) | |
42 os.dup2(stdout.fileno(), sys.stdout.fileno()) | |
43 os.dup2(stderr.fileno(), sys.stderr.fileno()) |