[Twisted-Python] Submitted for your approval

Jp Calderone exarkun at meson.dyndns.org
Wed Aug 28 19:36:42 EDT 2002


Because I use rebuild a _lot_ in one of my applications, I finally broke
down and write this addition to scripts/twistd.py.  I'm not sure if its
appropriate for inclusion in the repository (and if it is, it certainly
needs some cleanup first).


diff -a -c -t -r1.1 twistd.py
*** twisted/scripts/twistd.py   20 Aug 2002 02:31:59 -0000      1.1
--- twisted/scripts/twistd.py   28 Aug 2002 23:32:23 -0000
***************
*** 309,311 ****
--- 309,342 ----
      if usepid:
          os.unlink(config.opts['pidfile'])
      log.msg("Server Shut Down.")
+ 
+ 
+ def daemonize(logf = 'twistd.log'):
+     from twisted.python import log
+     if logf == '-':
+         print 'daemons cannot log to stdout'
+     else:
+         logPath = os.path.abspath(logf or 'twistd.log')
+         logFile = logfile.LogFile(os.path.basename(logPath), os.path.dirname(logPath))
+ 
+         # rotate logs on SIGUSR1
+         if os.name == "posix":
+             import signal
+             def rotateLog(signal, frame, logFile=logFile):
+                 logFile.rotate()
+             signal.signal(signal.SIGUSR1, rotateLog)
+ 
+         oldstdin = sys.stdin
+         oldstdout = sys.stdout
+         oldstderr = sys.stderr
+         log.startLogging(logFile)
+         sys.stdout.flush()
+ 
+         # Turn into a daemon.
+         if os.fork():   # launch child and...
+             os._exit(0) # kill off parent
+         os.setsid()
+         os.umask(077)
+         oldstdin.close()
+         oldstdout.close()
+         oldstderr.close()


An undaemonize() would be _really_ handy, too... Hmmm :)

 Jp

-- 
There are 10 kinds of people: those who understand binary and those who do
not.
--
 7:00pm up 99 days, 19:51, 5 users, load average: 0.03, 0.07, 0.02
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 237 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20020828/9a664427/attachment.pgp 


More information about the Twisted-Python mailing list