By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,586 Members | 1,719 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,586 IT Pros & Developers. It's quick & easy.

subprocess and PPID

P: n/a
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,
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?

Thanks,
Michele
Nov 5 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Nov 5, 5:12*pm, Michele Petrazzo <michele.petra...@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,
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.

-srp
>
Thanks,
Michele
Nov 5 '08 #2

P: n/a
On Wed, 5 Nov 2008 08:19:34 -0800 (PST), sa*********@gmail.com <sa*********@gmail.comwrote:
On Nov 5, 5:12*pm, Michele Petrazzo <michele.petra...@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!
Nov 5 '08 #3

P: n/a
In message <ge**********@nnrp-beta.newsland.it>, Michele Petrazzo wrote:
I have a code that execute into a "Popen" a command (ssh). I need that,
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?
There is a Linux-specific system call that says it does this (haven't
tried). See the prctl(2) man page.
Nov 6 '08 #4

P: n/a
Jorgen Grahn wrote:
On Wed, 5 Nov 2008 08:19:34 -0800 (PST), sa*********@gmail.com
<sa*********@gmail.comwrote:
>On Nov 5, 5:12 pm, Michele Petrazzo
<michele.petra...@TOGLIunipex.itwrote:
>>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?
Popen is the default python Popen:

from subprocess import Popen, PIPE

cmd = "ssh -C -N -i keys/id_rsa_key -L remote:ip:local who@where"

cmd_p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)

Do you read from it or write to it? If you do both, you should use
something like the third-party module pexpect instead.
Nothing. I do only a tunnel
>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".
Interesting. So, how I have to modify my code (if I can)? Add an stdin?
/Jorgen
Thanks,
Michele
Nov 6 '08 #5

P: n/a
Lawrence D'Oliveiro wrote:
In message <ge**********@nnrp-beta.newsland.it>, Michele Petrazzo
wrote:
>I have a code that execute into a "Popen" a command (ssh). I need
that, 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?

There is a Linux-specific system call that says it does this (haven't
tried). See the prctl(2) man page.
Just seen. It can be, bust since I cannot modify the child process and
this syscall must be called from the child, I cannot use it.

Thanks,
Michele
Nov 6 '08 #6

P: n/a
sa*********@gmail.com wrote:
>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.
Like I do ;)
If the parent is doing a uncontrolled exit (say via a SIGKILL
signal), you can't really do anything.
The only thing that I thought it's to use an external resource, like a
..pid file where I save the child pid(s) and, on the next parent startup
control the file and kill the process, if any
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.
Like said, I haven't the control of the sources, so I can't.

Thanks,
Michele
Nov 6 '08 #7

P: n/a
In message <ge**********@nnrp-beta.newsland.it>, Michele Petrazzo wrote:
Lawrence D'Oliveiro wrote:
>See the prctl(2) man page.

Just seen. It can be, bust since I cannot modify the child process and
this syscall must be called from the child, I cannot use it.
You do the fork and then the exec, right? So do the prctl in-between.
Nov 6 '08 #8

P: n/a
On Nov 6, 3:09*pm, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealandwrote:
In message <geu8k4$ce...@nnrp-beta.newsland.it>, Michele Petrazzo wrote:
Lawrence D'Oliveiro wrote:
See the prctl(2) man page.
Just seen. It can be, bust since I cannot modify the child process and
this syscall must be called from the child, I cannot use it.

You do the fork and then the exec, right? So do the prctl in-between.
You could also write a wrapper program that does a prctl and then
exec(actual command). Infact you could use a wrapper program to
portably poll for the parent if you dont want to prctl(); invoke this
wrapper from python, the wrapper can then invoke your actual command.

-srp
Nov 6 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.