comparison roundup/scripts/roundup_server.py @ 1072:88ded00fa0e0

better daemonification
author Richard Jones <richard@users.sourceforge.net>
date Sat, 07 Sep 2002 22:46:19 +0000
parents 1890c611de08
children e5826025eeb7
comparison
equal deleted inserted replaced
1071:c08b3820edd1 1072:88ded00fa0e0
14 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, 14 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
15 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 15 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
16 # 16 #
17 """ HTTP Server that serves roundup. 17 """ HTTP Server that serves roundup.
18 18
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 $
20 """ 20 """
21 21
22 # python version check 22 # python version check
23 from roundup import version_check 23 from roundup import version_check
24 24
183 pairs on the command-line. For convenience, you may edit the 183 pairs on the command-line. For convenience, you may edit the
184 ROUNDUP_INSTANCE_HOMES variable in the roundup-server file instead. 184 ROUNDUP_INSTANCE_HOMES variable in the roundup-server file instead.
185 ''')%locals() 185 ''')%locals()
186 sys.exit(0) 186 sys.exit(0)
187 187
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
188 def run(): 223 def run():
189 hostname = '' 224 hostname = ''
190 port = 8080 225 port = 8080
191 pidfile = None 226 pidfile = None
192 logfile = None 227 logfile = None
245 sys.argv = sys.argv[:1] 280 sys.argv = sys.argv[:1]
246 address = (hostname, port) 281 address = (hostname, port)
247 282
248 # fork? 283 # fork?
249 if pidfile: 284 if pidfile:
250 pid = os.fork() 285 daemonize(pidfile)
251 if pid: 286
252 print 'forking', pid 287 # redirect stdout/stderr to our logfile
253 open(pidfile, 'w').write(str(pid))
254 return
255
256 # redirect stdout/stderr
257 if logfile: 288 if logfile:
258 sys.stdout = sys.stderr = open(logfile, 'a') 289 sys.stdout = sys.stderr = open(logfile, 'a')
259 290
260 httpd = BaseHTTPServer.HTTPServer(address, RoundupRequestHandler) 291 httpd = BaseHTTPServer.HTTPServer(address, RoundupRequestHandler)
261 print _('Roundup server started on %(address)s')%locals() 292 print _('Roundup server started on %(address)s')%locals()
264 if __name__ == '__main__': 295 if __name__ == '__main__':
265 run() 296 run()
266 297
267 # 298 #
268 # $Log: not supported by cvs2svn $ 299 # $Log: not supported by cvs2svn $
300 # Revision 1.7 2002/09/04 07:32:55 richard
301 # add daemonification
302 #
269 # Revision 1.6 2002/08/30 08:33:28 richard 303 # Revision 1.6 2002/08/30 08:33:28 richard
270 # new CGI frontend support 304 # new CGI frontend support
271 # 305 #
272 # Revision 1.5 2002/03/14 23:59:24 richard 306 # Revision 1.5 2002/03/14 23:59:24 richard
273 # . #517734 ] web header customisation is obscure 307 # . #517734 ] web header customisation is obscure

Roundup Issue Tracker: http://roundup-tracker.org/