PID management with popen and spawn

EEK eekfunster at
Thu Sep 25 17:36:00 CEST 2008


 My goal is to start and stop separate Linux processes from a python
program by specific PID.  The output of these processes needs to have
their stderr and stdout piped to a particular file, respectively.

 I've been able to make this work with subprocess.Popen only if the
shell variable is set to False but I cannot pipe the outputs of the

 When using subprocess.Popen with shell=True, I believe the returned
PID is of the shell that opens the target process, not the target
process itself.  Therfore, I cannot stop the target process for the
returned PID is not of that process.

 Spawn will work better but I need the target application to pipe it's
stderr and stdout to a file.  Here I have troubles.

 Any help appreciated.

 prgm = program I want to run (compiled C)
 logfile = file I want to pipe output to

 What I need to do based on a simple shell call:  program >& logfile
(yes, that's it)

 proc = subprocess.Popen("program >&  logfile", shell=True,

 The above spawns the new process but is not of 'program'.
Therefore, my python program cannot kill/stop it if needed.

 pidNum = os.spawnle(os.P_NOWAIT, "program >&  logfile",
pidName ,os.environ)
 This does not work.  "File not found"



More information about the Python-list mailing list