Ubuntu pass information to a text file during process execution
Question: My issue is that I have a function call it function1 which runs indefinitely.
The function itself echo's "hello" every 1 second. Using the command:

    
    
    function1 >> temp.txt
    

Every 1 second if I am to view the file temp.txt I should see a new line with
"hello". However what's happening is that the function doesn't write to the
file until its finished running. Therefore if I am to kill the function
nothing will be written. Does anyone know of something I can do in this
situation that will basically dynamically update temp.txt?

I have a python script set up the following way:

    
    
    while [1>0]:
       print "hello"
       time.sleep(1)
    

Therefore this will always be running. Now I call this in a bash script

    
    
    python pythonscript.py >> temp.txt
    

If I `kill` the process no data is logged.


Answer: You are going to want to flush the buffer after each print.

    
    
    import time
    import sys
    
    while [1>0]:
        print "hello"
        sys.stdout.flush()
        time.sleep(1)
    

This will also work on other operating systems.

