[Twisted-Python] Submitted for your approval
![](https://secure.gravatar.com/avatar/7b7a2c0c3c3533817e28c9b2e6379695.jpg?s=120&d=mm&r=g)
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
participants (1)
-
Jp Calderone