473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

subprocess and PPID

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
8 3931
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc Carter | last post by:
I am trying to rewrite a PERL automation which started a "monitoring" application on many machines, via RSH, and then multiplexed their collective outputs to stdout. In production there are lots...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
0
by: alf | last post by:
Hi, I need a help with explaining following behavior. Although it is not python issue per say, python helped me to write sample programs and originally I encountered the issue using python...
10
by: JD | last post by:
Hi, I want send my jobs over a whole bunch of machines (using ssh). The jobs will need to be run in the following pattern: (Machine A) (Machine B) (Machine C) Job A1 Job B1 ...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.