473,386 Members | 1,830 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,386 software developers and data experts.

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 2421
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****@unpythonic.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.close()
sys.stderr.close()

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****@unpythonic.net wrote in message news:<ma*************************************@pyth on.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.programmer 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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.