Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
Please include the source/context of function1. –  izx Jul 20 '12 at 20:05
add comment

1 Answer 1

up vote 1 down vote accepted

Bash doesn't buffer, but python will buffer if output isn't going to a terminal (because it's more efficient), so you'll want to flush stdout after every line.

#!/usr/bin/env python
import sys
import time
while True:
    print "hello"
    sys.stdout.flush()
    time.sleep(1)
share|improve this answer
    
Works perfectly thank you! –  Matt Stokes Jul 23 '12 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.