PID management with popen and spawn

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Sep 30 02:16:11 EDT 2008


En Thu, 25 Sep 2008 12:36:00 -0300, EEK <eekfunster at gmail.com> escribió:

>  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
> targets.
>
>  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.

Exactly.

>  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)

Use the stdout and stderr arguments. Something like this (untested):

f = open(logfile, "w")
proc = subprocess.Popen(["program","with","arguments"], stdout=f, stderr=f)

-- 
Gabriel Genellina




More information about the Python-list mailing list