简体   繁体   中英

Python hangs over ssh

I'm trying to a run a python script directly over ssh like this:

ssh hostname python_script

Unfortunately nothing happens after python starts, and in fact the python process that is created remotely stays "alive" even after I disconnect from SSH. The same thing happens if I try to start the python interpreter, but other commands work fine.

Try ssh -t hostname python_script . By default, ssh doesn't allocate a pseudo-tty to interact with when it's given a program to run (although it does if you just do ssh hostname ); -t tells it to do so.

ssh -t is a good suggestion.

You might also try sprinkling print statements/functions in your code, writing to some file in /var/tmp or whatever, to see what it's doing.

Another way of seeing what a process is doing is to use something like Linux' strace: http://stromberg.dnsalias.org/~strombrg/debugging-with-syscall-tracers.html

EG: ssh remote.host.com 'strace -f -o /var/tmp/my_script.strace my_script'. Then inspect /var/tmp/my_script.strace to see what it's stuck on. Reading strace output isn't always simple, but at least it's interesting. :)

SSH isn't going to cause problems running a python script. In general the things to watch out for are environment variables changing and any expectation on standard input/output as these can cause symptoms similar to what you described. Running python directly will hang as python expects to be able to interact with stdin/stdout if not running a script.

An easy way to test that the basic environment is working is to create a test program (test.py) containing:

print "foo"

then

ssh hostname python test.py

you should get "foo" as a response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM