def createDaemon():
try:<EOL><INDENT>pid = os.fork()<EOL><DEDENT>except OSError as e:<EOL><INDENT>raise Exception("<STR_LIT>" % (e.strerror, e.errno))<EOL><DEDENT>if (pid == <NUM_LIT:0>):   <EOL><INDENT>os.setsid()<EOL>try:<EOL><INDENT>pid = os.fork()    <EOL><DEDENT>except OSError as e:<EOL><INDENT>raise Exception("<STR_LIT>" % (e.strerror, e.errno))<EOL><DEDENT>if (pid == <NUM_LIT:0>):    <EOL><INDENT>os.chdir(WORKDIR)<EOL>os.umask(UMASK)<EOL><DEDENT>else:<EOL><INDENT>os._exit(<NUM_LIT:0>)    <EOL><DEDENT><DEDENT>else:<EOL><INDENT>os._exit(<NUM_LIT:0>)   <EOL><DEDENT>import resource      <EOL>maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[<NUM_LIT:1>]<EOL>if (maxfd == resource.RLIM_INFINITY):<EOL><INDENT>maxfd = MAXFD<EOL><DEDENT>for fd in range(<NUM_LIT:0>, maxfd):<EOL><INDENT>try:<EOL><INDENT>os.close(fd)<EOL><DEDENT>except OSError:   <EOL><INDENT>pass<EOL><DEDENT><DEDENT>os.open(REDIRECT_TO, os.O_RDWR)  <EOL>os.dup2(<NUM_LIT:0>, <NUM_LIT:1>)            <EOL>os.dup2(<NUM_LIT:0>, <NUM_LIT:2>)            <EOL>return(<NUM_LIT:0>)<EOL>
Detach a process from the controlling terminal and run it in the
    background as a daemon.