# HG changeset patch # User Atul Varma # Date 1346560821 0 # Node ID a1e629ed86fc139d28f2bc26cdb2c9740f56e8d4 # Parent 7e11415cf2728f6f5fd5c585777c543e7687a270 Added 'env' and 'logFile' options to Process constructor. diff -r 7e11415cf272 -r a1e629ed86fc ProcessManager.py --- a/ProcessManager.py Sun Sep 04 18:36:28 2011 +0000 +++ b/ProcessManager.py Sun Sep 02 04:40:21 2012 +0000 @@ -158,21 +158,29 @@ program, args, workingDir, + logFile = None, + env = None, uid = None, gid = None, stopSignal = None ): """ Creates a process with the given name/identifier, description, - program executable path, argument tuple, and working + program executable path, argument tuple, environment, and working directory. When it is run, it will run with the given user and group ID privileges. When it is stopped, the given signal will be sent to tell it to do so. + + If logFile is provided, the stdout and stderr of the process + will be redirected to the filename specified by logFile. """ - if stopSignal == None: + if stopSignal is None: import signal stopSignal = signal.SIGKILL + if env is None: + env = {} + self.name = name self.desc = desc self.program = program @@ -180,6 +188,8 @@ self.args.extend( args ) self.workingDir = workingDir self.stopSignal = stopSignal + self.env = env + self.logFile = logFile if gid and uid: import grp @@ -307,21 +317,35 @@ nullFile = os.open( "/dev/null", os.O_RDWR ) + if self.logFile: + logFile = os.open( self.logFile, + os.O_APPEND | os.O_CREAT | os.O_WRONLY ) + else: + logFile = nullFile + # Replace stdin. os.dup2( nullFile, 0 ) # Replace stdout if not _options.enableStdout: - os.dup2( nullFile, 1 ) + os.dup2( logFile, 1 ) # Replace stderr if not _options.enableStderr: - os.dup2( nullFile, 2 ) + os.dup2( logFile, 2 ) os.close( nullFile ) - + + sys.stdout.write( 'Launching %s with args %s on %s.\n' % + (self.program, self.args, time.asctime()) ) + sys.stdout.flush() + + env = {} + env.update( os.environ ) + env.update( self.env ) + # Launch the program. - os.execv( self.program, self.args ) + os.execve( self.program, self.args, env ) else: # We're the parent process. pid = forkResult