SSH output isn’t line buffered?

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)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments