carriagereturn

python daemon

import os
import sys

class TestDaemon:

def run_daemon(self):
# Disconnect from tty
try:
pid = os.fork()
if pid>0:
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed", e
sys.exit(1)

os.setsid()
os.umask(0)

# Second fork
try:
pid = os.fork()
if pid>0:
open(PIDFILE,'w').write(str(pid))
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed", e
sys.exit(1)

self.run()

def run(self):
// Your code here

if __name__ == "__main__":
c = TestDaemon()
c.run_daemon()

Inspiration: Anand Balachandran Pillai