PID management with popen and spawn

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EEK

    PID management with popen and spawn


    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.Pope n only if the
    shell variable is set to False but I cannot pipe the outputs of the
    targets.

    When using subprocess.Pope n 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.Pope n("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




  • Gabriel Genellina

    #2
    Re: PID management with popen and spawn

    En Thu, 25 Sep 2008 12:36:00 -0300, EEK <eekfunster@gma il.comescribió:
    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.Pope n only if the
    shell variable is set to False but I cannot pipe the outputs of the
    targets.
    >
    When using subprocess.Pope n 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.Pope n(["program","with ","argument s"], stdout=f, stderr=f)

    --
    Gabriel Genellina

    Comment

    Working...