__author__ = "Chad J. Schroeder" __copyright__ = "Copyright (C) 2005 Chad J. Schroeder" __revision__ = "$Id$" __version__ = "0.2" # Standard Python modules. import os # Miscellaneous OS interfaces. import sys # System-specific parameters and functions. UMASK = 0 WORKDIR = "/" # Default maximum for the number of available file descriptors. MAXFD = 1024 # The standard I/O file descriptors are redirected to /dev/null by default. # If you need to debug the launching of daemons, direct to a file instead. if (hasattr(os, "devnull")): REDIRECT_TO = os.devnull # REDIRECT_TO= "/Users/terri/daemon.txt" else: REDIRECT_TO = "/dev/null" # REDIRECT_TO= "/Users/terri/daemon.txt" def createDaemon(): """Detach a process from the controlling terminal and run it in the background as a daemon. """ try: pid = os.fork() except OSError, e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): # The first child. os.setsid() try: pid = os.fork() # Fork a second child. except OSError, e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): # The second child. os.chdir(WORKDIR) os.umask(UMASK) else: # exit() or _exit()? See comment in original version in svn. os._exit(0) else: os._exit(0) # Exit parent of the first child. import resource # Resource usage information. maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if (maxfd == resource.RLIM_INFINITY): maxfd = MAXFD # Iterate through and close all file descriptors. for fd in range(0, maxfd): try: os.close(fd) except OSError: # ERROR, fd wasn't open to begin with (ignored) pass os.open(REDIRECT_TO, os.O_RDWR | os.O_CREAT) # standard input (0) # Duplicate standard input to standard output and standard error. os.dup2(0, 1) # standard output (1) os.dup2(0, 2) # standard error (2) return(0) if __name__ == "__main__": if len(sys.argv) < 2 or ((sys.argv[1] != "registry") and (sys.argv[1] != "propsvc")): sys.exit('Expecting "registry" or "propsvc" as argument') service = sys.argv[1] cipresRoot = os.environ.get('CIPRES_ROOT') if not cipresRoot: sys.exit('CIPRES_ROOT env var must be set') try: retCode = createDaemon() except: sys.exit(1) # The code, as is, will create a new file in the root directory, when # executed with superuser privileges. The file will contain the following # daemon related process parameters: return code, process ID, parent # process group ID, session ID, user ID, effective user ID, real group ID, # and the effective group ID. Notice the relationship between the daemon's # process ID, process group ID, and its parent's process ID. # try: # open("/Users/terri/daemon.log", "w+").write(procParams + "\n") # except: # sys.exit(2) import subprocess jarfile = os.path.join(cipresRoot, 'lib', 'cipres', 'cipres-classpath.jar') print 'jarfile is ', jarfile if service == 'registry': pid = subprocess.Popen(["java", "-DfixedPort=1", "-ea", "-classpath", jarfile, "org.cipres.registry.RegistryApp" ]).pid else: pid = subprocess.Popen(["java", "-ea", "-classpath", jarfile, "org.cipres.properties.PropertiesApp" ]).pid print 'launched ', service, ' on ', pid sys.exit(retCode)