Skip to content

Commit e2ac797

Browse files
author
Richard Jones
committed
better daemonification
1 parent 3da0b58 commit e2ac797

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

roundup/scripts/roundup_server.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
""" HTTP Server that serves roundup.
1818
19-
$Id: roundup_server.py,v 1.7 2002-09-04 07:32:55 richard Exp $
19+
$Id: roundup_server.py,v 1.8 2002-09-07 22:46:19 richard Exp $
2020
"""
2121

2222
# python version check
@@ -185,6 +185,41 @@ def usage(message=''):
185185
''')%locals()
186186
sys.exit(0)
187187

188+
def daemonize(pidfile):
189+
''' Turn this process into a daemon.
190+
- make sure the sys.std(in|out|err) are completely cut off
191+
- make our parent PID 1
192+
193+
Write our new PID to the pidfile.
194+
195+
From A.M. Kuuchling (possibly originally Greg Ward) with
196+
modification from Oren Tirosh, and finally a small mod from me.
197+
'''
198+
# Fork once
199+
if os.fork() != 0:
200+
os._exit(0)
201+
202+
# Create new session
203+
os.setsid()
204+
205+
# Second fork to force PPID=1
206+
pid = os.fork()
207+
if pid:
208+
pidfile = open(pidfile, 'w')
209+
pidfile.write(str(pid))
210+
pidfile.close()
211+
os._exit(0)
212+
213+
os.chdir("/")
214+
os.umask(0)
215+
216+
# close off sys.std(in|out|err), redirect to devnull so the file
217+
# descriptors can't be used again
218+
devnull = os.open('/dev/null', 0)
219+
os.dup2(devnull, 0)
220+
os.dup2(devnull, 1)
221+
os.dup2(devnull, 2)
222+
188223
def run():
189224
hostname = ''
190225
port = 8080
@@ -247,13 +282,9 @@ def run():
247282

248283
# fork?
249284
if pidfile:
250-
pid = os.fork()
251-
if pid:
252-
print 'forking', pid
253-
open(pidfile, 'w').write(str(pid))
254-
return
285+
daemonize(pidfile)
255286

256-
# redirect stdout/stderr
287+
# redirect stdout/stderr to our logfile
257288
if logfile:
258289
sys.stdout = sys.stderr = open(logfile, 'a')
259290

@@ -266,6 +297,9 @@ def run():
266297

267298
#
268299
# $Log: not supported by cvs2svn $
300+
# Revision 1.7 2002/09/04 07:32:55 richard
301+
# add daemonification
302+
#
269303
# Revision 1.6 2002/08/30 08:33:28 richard
270304
# new CGI frontend support
271305
#

0 commit comments

Comments
 (0)