comparison ProcessManager.py @ 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
comparison
equal deleted inserted replaced
5:7e11415cf272 6:a1e629ed86fc
156 name, 156 name,
157 desc, 157 desc,
158 program, 158 program,
159 args, 159 args,
160 workingDir, 160 workingDir,
161 logFile = None,
162 env = None,
161 uid = None, 163 uid = None,
162 gid = None, 164 gid = None,
163 stopSignal = None ): 165 stopSignal = None ):
164 """ 166 """
165 Creates a process with the given name/identifier, description, 167 Creates a process with the given name/identifier, description,
166 program executable path, argument tuple, and working 168 program executable path, argument tuple, environment, and working
167 directory. When it is run, it will run with the given user 169 directory. When it is run, it will run with the given user
168 and group ID privileges. When it is stopped, the given signal 170 and group ID privileges. When it is stopped, the given signal
169 will be sent to tell it to do so. 171 will be sent to tell it to do so.
170 """ 172
171 173 If logFile is provided, the stdout and stderr of the process
172 if stopSignal == None: 174 will be redirected to the filename specified by logFile.
175 """
176
177 if stopSignal is None:
173 import signal 178 import signal
174 stopSignal = signal.SIGKILL 179 stopSignal = signal.SIGKILL
180
181 if env is None:
182 env = {}
175 183
176 self.name = name 184 self.name = name
177 self.desc = desc 185 self.desc = desc
178 self.program = program 186 self.program = program
179 self.args = [ program ] 187 self.args = [ program ]
180 self.args.extend( args ) 188 self.args.extend( args )
181 self.workingDir = workingDir 189 self.workingDir = workingDir
182 self.stopSignal = stopSignal 190 self.stopSignal = stopSignal
191 self.env = env
192 self.logFile = logFile
183 193
184 if gid and uid: 194 if gid and uid:
185 import grp 195 import grp
186 import pwd 196 import pwd
187 197
305 315
306 os.chdir( self.workingDir ) 316 os.chdir( self.workingDir )
307 317
308 nullFile = os.open( "/dev/null", os.O_RDWR ) 318 nullFile = os.open( "/dev/null", os.O_RDWR )
309 319
320 if self.logFile:
321 logFile = os.open( self.logFile,
322 os.O_APPEND | os.O_CREAT | os.O_WRONLY )
323 else:
324 logFile = nullFile
325
310 # Replace stdin. 326 # Replace stdin.
311 os.dup2( nullFile, 0 ) 327 os.dup2( nullFile, 0 )
312 328
313 # Replace stdout 329 # Replace stdout
314 if not _options.enableStdout: 330 if not _options.enableStdout:
315 os.dup2( nullFile, 1 ) 331 os.dup2( logFile, 1 )
316 332
317 # Replace stderr 333 # Replace stderr
318 if not _options.enableStderr: 334 if not _options.enableStderr:
319 os.dup2( nullFile, 2 ) 335 os.dup2( logFile, 2 )
320 336
321 os.close( nullFile ) 337 os.close( nullFile )
322 338
339 sys.stdout.write( 'Launching %s with args %s on %s.\n' %
340 (self.program, self.args, time.asctime()) )
341 sys.stdout.flush()
342
343 env = {}
344 env.update( os.environ )
345 env.update( self.env )
346
323 # Launch the program. 347 # Launch the program.
324 os.execv( self.program, self.args ) 348 os.execve( self.program, self.args, env )
325 else: 349 else:
326 # We're the parent process. 350 # We're the parent process.
327 pid = forkResult 351 pid = forkResult
328 f = open( self._pidfile(), "w" ) 352 f = open( self._pidfile(), "w" )
329 f.write( "%d" % pid ) 353 f.write( "%d" % pid )