/
daemonize.py
58 lines (52 loc) · 1.71 KB
/
daemonize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Copyright (C) 2007-2011 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Daemonizing a process.
"""
import sys
import os
def _redirect_output(filename):
out_log = open(filename, 'a+', 0)
err_log = open(filename, 'a+', 0)
dev_null = open(os.devnull, 'r')
os.close(sys.stdin.fileno())
os.close(sys.stdout.fileno())
os.close(sys.stderr.fileno())
os.dup2(out_log.fileno(), sys.stdout.fileno())
os.dup2(err_log.fileno(), sys.stderr.fileno())
os.dup2(dev_null.fileno(), sys.stdin.fileno())
def daemonize():
"""
Forks and daemonizes the current process. Does not automatically track
the process id; to do this, use L{Exscript.util.pidutil}.
"""
sys.stdout.flush()
sys.stderr.flush()
# UNIX double-fork magic. We need to fork before any threads are
# created.
pid = os.fork()
if pid > 0:
# Exit first parent.
sys.exit(0)
# Decouple from parent environment.
os.chdir('/')
os.setsid()
os.umask(0)
# Now fork again.
pid = os.fork()
if pid > 0:
# Exit second parent.
sys.exit(0)
_redirect_output(os.devnull)