Use the function goDaemon just before starting the server loop:<br><br>###############Begin Module#######################<br>import os<br>import sys<br># Default daemon parameters.<br># File mode creation mask of the daemon.<br>
UMASK = 0<br># Default working directory for the daemon.<br>WORKDIR = "/"<br># Default maximum for the number of available file descriptors.<br>MAXFD = 1024<br># The standard I/O file descriptors are redirected to /dev/null by default.<br>
if (hasattr(os, "devnull")):<br> REDIRECT_TO = os.devnull<br>else:<br> REDIRECT_TO = "/dev/null"<br>def goDaemon():<br> try:<br> pid = os.fork()<br> except OSError, e:<br> raise Exception, "%s [%d]" % (e.strerror, e.errno)<br>
if (pid == 0): # The first child.<br> os.setsid()<br> try:<br> pid = os.fork() # Fork a second child.<br> except OSError, e:<br> raise Exception, "%s [%d]" % (e.strerror, e.errno)<br>
if (pid == 0): # The second child.<br> os.chdir(WORKDIR)<br> os.umask(UMASK)<br> else:<br> os._exit(0)<br> else:<br> os._exit(0) # Exit parent of the first child.<br> import resource<br>
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]<br> if (maxfd == resource.RLIM_INFINITY):<br> maxfd = MAXFD<br> for fd in range(0, maxfd):<br> try:<br> os.close(fd)<br> except OSError: # ERROR, fd wasn't open to begin with (ignored)<br>
pass<br> os.open(REDIRECT_TO, os.O_RDWR) # standard input (0)<br> os.dup2(0, 1) # standard output (1)<br> os.dup2(0, 2) # standard error (2)<br> return(0)<br>
###############End Module#######################<br><br><div class="gmail_quote">On Tue, Feb 26, 2008 at 12:06 PM, bharath venkatesh <<a href="mailto:bharathv6@gmail.com">bharathv6@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
hi ..<br> hi i want a program to start running as daemon in background .. my program is server listen to a client ... so i want to make that program run as daemon .. when i execute the program the program for ex server.py it should automatically start running as daemon in the background even if i close the terminal it shouldn't stop executing.. (right now if i close the terminal the process exits) can any one tell how to do it in python as i have implemented the server in python ...<br>
<br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div>-- <br>Regards--<br>Rishi Pathak<br>