changeset 70:ee0ee8a6c40d

merge
author Matt Johnston <matt@ucc.asn.au>
date Wed, 27 Jun 2012 23:46:12 +0800
parents a26da94f3164 (diff) a8ff20f15734 (current diff)
children a1edade2b9cd
files
diffstat 2 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/server/ts.py	Wed Jun 27 23:41:50 2012 +0800
+++ b/server/ts.py	Wed Jun 27 23:46:12 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:
--- a/server/utils.py	Wed Jun 27 23:41:50 2012 +0800
+++ b/server/utils.py	Wed Jun 27 23:46:12 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)
+
+