LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2005, 02:19 PM   #1
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
ssh changes the way python script behaves


I wrote a python script that checks for new media on my windows box, if it is new it copys it over to my standalone freevo jukebox. Below is a small part of the script that does the actually copying
Code:
source = "/mnt/samba/%s" %i
destination = "/mnt/media/movies"
print "copying %s to %s" %(i, destination)
shutil.copy2(source, destination)
when I ssh into the freevo box then run the script it behaves like I think it shoud. first it runs the print function, then runs the shutil.copy2 function.

here is where it is behaving buggy. if I run the script directly using a command like this
Code:
ssh root@192.168.1.103 /usr/local/bin/sambacopy.py
it does not print to the screen until after it has finished copying. I have found a solution of using sys.stdout.flush() directly after the print statment
Code:
source = "/mnt/samba/%s" %i
destination = "/mnt/media/movies"
print "copying %s to %s" %(i, destination)
sys.stdout.flush()
shutil.copy2(source, destination)
why is the flush function needed when I run the script directly, but not if I ssh into a shell , then run the script
 
Old 11-15-2005, 09:08 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
I think the reason may be that in your second case, ssh is running the command as a subprocess, rather than through a shell. The upshot is that the output is buffered until the command completes.

Your call to "sys.stdout.flush()" flushes the buffer, so the output is printed immediately.

A quick test to see if my guess is correct would be to put this script on your server:
Code:
#!/usr/bin/python
import time
time.sleep(5)
print "some text"
time.sleep(5)
print "some text"
Now run it through ssh. Does it wait 10 seconds and print both messages?

Last edited by bulliver; 11-15-2005 at 09:13 PM.
 
Old 11-15-2005, 09:10 PM   #3
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
sounds reasonable :-)
 
Old 11-16-2005, 02:19 PM   #4
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
the script tested as you thought. it waits 10 seconds then prints both to screen. I still do not understand why running the command as a subprocess would cause all of the output not to be flushed until the script finishes.

Last edited by shanenin; 11-16-2005 at 02:33 PM.
 
Old 11-16-2005, 03:22 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
I still do not understand why running the command as a subprocess would cause all of the output not to be flushed until the script finishes
I don't think there is any actual technical reason for it, it's just the way the OpenSSH developers wrote it...

Again, I'm really just guessing here though
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python commands over ssh Kedelfor Programming 7 10-18-2005 04:07 AM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM
Help with small Python Script cmfarley19 Programming 8 05-02-2005 08:10 AM
ssh behaves different for root and users lopette Linux - Software 1 11-20-2004 06:09 AM
python script LinuxLala Programming 14 04-07-2004 06:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration