44

I'm running a script on a remote machine like this:

ssh $host "pip install -r /path/to/requirements.txt"

But the output isn't line buffered; instead of seeing one line returned at a time, all the lines (~10) are all printed at once as the connection terminates.

What's up with this? Is there any way to force them to be line buffered?

(also, to state the obvious: when I ssh into $host and run the command “manually”, the output is line buffered, as expected)

3 Answers3

54

Use ssh -t ... to force a pseudo-tty allocation (which is what you get when you log in normally via ssh.)

Ryan Fox
  • 656
25

To expand a little bit on Ryan Fox's answer: Many programs (most? - it's the default for any C program) line-buffer stdout when they're talking to a terminal, but fully buffer it otherwise. (The C standard specifies that stdout is initially fully buffered when it "can be determined not to refer to an interactive device".)

So what you're seeing is that the output of the program you're running remotely (as given to stdout) isn't line-buffered; ssh is just passing through what it gets when it gets it. (I think ssh actually does no buffering at all on its output - that would be the least magical way to make sure that the user sees what the remote program intended.)

9

To expand even more on Ryan Fox's answer, ssh -t didn't work for me either, but ssh -tt did. See the ssh man page about -t:

Multiple -t options force tty allocation, even if ssh has no local tty

Prvt_Yadav
  • 5,882