chpatrick
chpatrick

Reputation: 481

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.

Upvotes: 3

Views: 2870

Answers (3)

user1277476
user1277476

Reputation: 2909

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. :)

Upvotes: 1

twalberg
twalberg

Reputation: 62379

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.

Upvotes: 7

Clarus
Clarus

Reputation: 2338

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.

Upvotes: 0

Related Questions