473,800 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python and ssh

Jim
Hi,

I'm fairly new to python, and unix systems in general.

I have a script where I am trying to launch a remote process. What
I'm doing is something like:

from host machine:
p = popen2.POpen4(' ssh user@remote ssh /home/remote.py cmd arg1 arg2
....' )

so the remote machine has a remote.py script which gets the arguments
that i want to execute.

in the remote.py script, i pretty much just do a
pid = os.spawnv( os.P_NOWAIT, [cmd, cmdname, arg1, arg2, ... ] )
then i exit the script, i also send the pid over a socket before
exiting.

I noticed that even thought the python process dies, ssh still 'knows'
about the spawned process, so the ssh session is still going.

My question is can I rely on this behavior? How did ssh know about
the spawned process?

Thank you,
Jul 18 '05 #1
3 2438
You would have to ask the authors of ssh that question.

I *suspect* that sshd creates special file descriptors that communicate
with the process started on the remote machine as the user. Then it
enters a select loop. Once all the programs with access to that file
descriptor close it (for instance, by exiting), sshd detects this because
the file descriptors are "readable" according to select, but a read gets
0 bytes. When that happens, sshd closes down communication with ssh,
which exits.

Jeff
Jul 18 '05 #2
On Wed, 21 Jul 2004 je****@unpython ic.net wrote:
You would have to ask the authors of ssh that question.

I *suspect* that sshd creates special file descriptors that communicate
with the process started on the remote machine as the user. Then it
enters a select loop. Once all the programs with access to that file
descriptor close it (for instance, by exiting), sshd detects this because
the file descriptors are "readable" according to select, but a read gets
0 bytes. When that happens, sshd closes down communication with ssh,
which exits.


Building off of that, in order to get ssh to end the connection, the
spawned process will need to close the descriptors it inherited from
ssh:

import sys
sys.stdin.close ()
sys.stdout.clos e()
sys.stderr.clos e()

Basically, ssh is staying open because it knows that it's possible for the
spawned process to write to these streams, which would normally be
redirected over the ssh connection. By closing them, you're telling ssh
"I don't need to do any more input or output to/from the console", and ssh
(should) respond to this by closing the connection. (I say should because
I haven't tested this!)

Jul 18 '05 #3
je****@unpython ic.net wrote in message news:<ma******* *************** *************** @python.org>...
You would have to ask the authors of ssh that question.

I *suspect* that sshd creates special file descriptors that communicate
with the process started on the remote machine as the user. Then it
enters a select loop. Once all the programs with access to that file
descriptor close it (for instance, by exiting), sshd detects this because
the file descriptors are "readable" according to select, but a read gets
0 bytes. When that happens, sshd closes down communication with ssh,
which exits.


(You may be able to get away with a shell script that nohups your
python script, possibly disown'ing it as well)

If you're running under some flavor of Unix, it's also likely that ssh
has created a pty as a controlling terminal for your process group, in
which case you'll need to jump through a couple more hoops (setsid,
etc). The comp.unix.progr ammer FAQ has a section "How do I get my
program to act like a daemon?" which explains the steps required to
make this work:
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
It's written for C, but there are Python wrappers for all the relevant
syscalls (mostly in the os module)

If it is just the file descriptors, you probably want to close stdout
and stderr (and probably stdin) before launching the new process. You
want to replace them with new values (often /dev/null is handy here).
Best way to do this is to switch from os.spawnv to os.fork and
os.execv, and do the process setup for the new process in between
those two calls--but if you're on Windows that's not an option; if
you're planning on exiting immediately anyway, you may get away with
doing it in the main process before the spawnv call.

Sumner
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.