import os, sys, time

# demonize() is a function which makes the process calling it into a
# "demon". The process is detached from the current shell and will not
# receive hangup when the shell exits.
#
# Steinar Knutsen, 1997

def demonize():
	pid = os.fork()
	if not pid:
		otherpid = os.fork()
		if not otherpid:
			ppid = os.getppid()
			while ppid != 1:
				time.sleep(0.5)
				ppid = os.getppid()
			return
		else:
			os._exit(0)
	else:
		os.wait()
		sys.exit(0)
