changeset 6:a1e629ed86fc default tip

Added 'env' and 'logFile' options to Process constructor.
author Atul Varma <varmaa@toolness.com>
date Sun, 02 Sep 2012 04:40:21 +0000
parents 7e11415cf272
children
files ProcessManager.py
diffstat 1 files changed, 30 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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