#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    2
    Rep Power
    0

    Daemonize a python program


    Hi.
    I have written a python program ( a socket server)
    that i want to start in a background process with its own pid.

    I have a daemon written in python, wich forks off a child prosess.
    But i want to daemonize the socket server.

    Meaning i dont want to start it with >> python server.py

    But through the daemon.

    How can i do this ?
    The daemon is below
    Code:
    # source #http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
    import os              
    import sys             
    import signal           
    
    def createDaemon():
       
       try:
         
          pid = os.fork()
       except OSError, e:
          return((e.errno, e.strerror))     
    
       if (pid == 0):       
    
          os.setsid()
          signal.signal(signal.SIGHUP, signal.SIG_IGN)
    
       else:
          os._exit(0)         # Exit parent of the first child.
    
       
       try:
          maxfd = os.sysconf("SC_OPEN_MAX")
       except (AttributeError, ValueError):
          maxfd = 256   
    
       for fd in range(0, maxfd):
          try:
             os.close(fd)
          except OSError:   
             pass
    
       
       os.open("/dev/null", os.O_RDONLY)    # standard input (0)
       os.open("/dev/null", os.O_RDWR)       # standard output (1)
       os.open("/dev/null", os.O_RDWR)       # standard error (2)
    
       return(0)
    
    #if __name__ == "__main__":
    
    
     #  retCode = createDaemon()
       
       
       #os.system('python /home/admin/worm/window.py')
    
       #sys.exit(0)
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Instead of return(0), simply call the first line of the socketserver right there. Actually, the safest way is to do a double fork
    python Code:
     
    if __name__ == "__main__":
        # do the UNIX double-fork magic, see Stevens' "Advanced 
        # Programming in the UNIX Environment" for details (ISBN 0201563177)
        try: 
            pid = os.fork() 
            if pid > 0:
                # exit first parent
                sys.exit(0) 
        except OSError, e: 
            print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) 
            sys.exit(1)
     
        # decouple from parent environment
        os.chdir("/") 
        os.setsid() 
        os.umask(0) 
     
        # do second fork
        try: 
            pid = os.fork() 
            if pid > 0:
                # exit from second parent, print eventual PID before
                print "Daemon PID %d" % pid 
                sys.exit(0) 
        except OSError, e: 
            print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror) 
            sys.exit(1) 
     
        # start the daemon main loop
        main()

    Note the call to main() at the very end. That's where you should call the first function
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    2
    Rep Power
    0
    Thank you.

    I got it working

IMN logo majestic logo threadwatch logo seochat tools logo