PID management with popen and spawn

E

EEK

Hello,

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.

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,
env=os.environ)

The above spawns the new process but proc.pid 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"

Thanks,

EEK
 
G

Gabriel Genellina

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top