subprocess and PPID

Jorgen Grahn grahn+nntp at snipabacken.se
Wed Nov 5 12:08:11 EST 2008


On Wed, 5 Nov 2008 08:19:34 -0800 (PST), saju.pillai at gmail.com <saju.pillai at gmail.com> wrote:
> On Nov 5, 5:12 pm, Michele Petrazzo <michele.petra... at TOGLIunipex.it>
> wrote:
>> Hi all,
>> I believe that this is a *nix question, but since I'm developing in
>> python, I'm here.
>>
>> I have a code that execute into a "Popen" a command (ssh). I need that,

What's 'a "Popen"'? Is it os.popen or one of its variants?

Do you read from it or write to it?  If you do both, you should use
something like the third-party module pexpect instead.

>> if the python process die, the parent pid (PPID) of the child don't
>> become 1 (like I can seen on /proc/$pid$/status ), but it has to die,
>> following it's parent
>> It's possible in linux and with subprocess?
>
> AFAIK, there is no easy way to do this. If the parent python process
> is doing a controlled exit, just kill the child via close() on Popen()
> handle. If the parent is doing a uncontrolled exit (say via a SIGKILL
> signal), you can't really do anything.
>
> To reliably have the child exit when the parent exits, you would have
> to poll for the parent from the child and do a exit when the child
> detects that the parent has gone away.

But in the special case where he's feeding data into "ssh somewhere
somecmd" or pulling data from it, a crash of the parent should make
"somecmd" exit because it sees EOF, and thus make the ssh process end.
Sounds relatively risk-free -- but it depends on "somecmd".

/Jorgen

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.se>          R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list