# HG changeset patch # User Matt Johnston # Date 1341064846 -28800 # Node ID a1edade2b9cdc928ff6e680e4e597480e5b4eda0 # Parent ee0ee8a6c40d0f7af886011715af2b85406b0bf9# Parent 7d243ba2dd397fc1c9f0c1138ae8a0b8ae9ccfcf merge diff -r 7d243ba2dd39 -r a1edade2b9cd server/ts.py --- a/server/ts.py Fri Jun 29 22:46:59 2012 +0800 +++ b/server/ts.py Sat Jun 30 22:00:46 2012 +0800 @@ -25,6 +25,7 @@ import config from utils import monotonic_time, retry, readline, crc16 +import utils lightblue = None try: @@ -172,6 +173,9 @@ def main(): next_wake_time = 0 + if '--daemon' in sys.argv: + utils.cheap_daemon() + while True: sock = None try: diff -r 7d243ba2dd39 -r a1edade2b9cd server/utils.py --- a/server/utils.py Fri Jun 29 22:46:59 2012 +0800 +++ b/server/utils.py Sat Jun 30 22:00:46 2012 +0800 @@ -97,3 +97,31 @@ uc += 1 i += 1 return crc + +def cheap_daemon(): + print "Daemonising." + sys.stdout.flush() + sys.stderr.flush() + out = file('/dev/null', 'a+') + os.dup2(out.fileno(), sys.stdout.fileno()) + os.dup2(out.fileno(), sys.stderr.fileno()) + + try: + pid = os.fork() + if pid > 0: + sys.exit(0) + except OSError, e: + print>>sys.stderr, "Bad fork()" + sys.exit(1) + + os.setsid() + + try: + pid = os.fork() + if pid > 0: + sys.exit(0) + except OSError, e: + print>>sys.stderr, "Bad fork()" + sys.exit(1) + +