Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make shell output redirect (>) write while script is still running?

I wrote a short script that never terminates. This script continuously generates output that I have to check on every now and then. I'm running it on a lab computer through SSH, and redirecting the output to a file in my public_html folder on that machine.

python script.py > ~/public_html/results.txt

However, the results don't show up immediately when I refresh the address. The results show up when I terminate the program, but as I said, it doesn't halt by itself. Is that redirect (>) being lazy with with writing? Is there a way to continuously (or with an interval) update the results in the file?

Or is it the webserver that doesn't update the file while it is still being written?

like image 441
noio Avatar asked May 02 '10 11:05

noio


2 Answers

You need to flush the output sys.stdout.flush() (or smth) if you want to see it immediately. See this

like image 133
nc3b Avatar answered Nov 02 '22 23:11

nc3b


stdout is buffered, if not connected to terminal.

You can change this policy to line-buffering via stdbuf

stdbuf -oL python script.py > ~/public_html/results.txt

So you don't have to flush in your Python script and keep it IO efficient, if line-buffering is not required.

like image 26
Jürgen Hötzel Avatar answered Nov 03 '22 00:11

Jürgen Hötzel