mtimzh
mtimzh

Reputation: 13

Print SSH output line by line instead of everything at once

Say I have a server at 01.23.456.789 and run the following command:

ssh 01.23.456.789 "python3 -c 'import time; print(1); time.sleep(10); print(2);'"

It prints 1 and 2 simultaneously after 10 seconds. Is there any way to get the individual output immediately, so I would get 1 printed, wait for 10 seconds, and get 2 printed?

Upvotes: 1

Views: 602

Answers (1)

Socowi
Socowi

Reputation: 27215

Python buffers its outputs. You have to flush the buffer somehow, either from inside python or from outside using stdbuf -oL. You can also use python3 -u to automatically flush upon print.

ssh 01.23.456.789 "python3 -uc 'import time; print(1); time.sleep(10); print(2);'"

Upvotes: 1

Related Questions