Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a program running an infinite loop and will not terminate until receiving a CTRL+C signal. What I want to do is redirecting the output from this program into a file, say log for example. However, after I CTRL+Ced the program, nothing is recorded into log. I think it maybe some problem related to I/O buffer, but have no idea how to solve it. Am I on the right track? How to dump output from an infinite-loop program into a file?

share|improve this question
Have you tried shrinking the buffer to zero (or the min that they will allow)? Also, do you have acces to the source code? – soandos Nov 25 '11 at 21:00
You could pipe the output of your program to logger (see man logger). – u-punkt Nov 25 '11 at 21:13
How are you generating the output? Simple printf? – ott-- Nov 25 '11 at 21:18
Is this code you wrote? If so, this question belongs on Stack Overflow, not on Super User, though you should at least mention your programming language and preferably post some sample code that exhibits the problem. Note: do not repost, the question will be migrated automatically if enough users or a moderator push it along. – Gilles Nov 25 '11 at 21:29
@soandos sorry, the source code is not available. – Summer_More_More_Tea Nov 26 '11 at 4:55
up vote 5 down vote accepted

If your program is using the standard output functions (e.g. puts, printf and friends from stdio.h in C, cout << … in C++, print in many high-level languages), then its output is buffered: characters accumulate in a memory zone which is called a buffer; when there's too much data in the buffer, the contents of the buffer is printed (it's “flushed”) and the buffer becomes empty (ready to be filled again). If your program doesn't produce much output, it may not have filled its buffer yet.

When you kill your program with Ctrl+C, unless you've set a signal handler for SIGINT, it just dies. The contents of any unflushed buffer is lost.

You have three possibilities:

share|improve this answer

A script (with an infinite loop or not) should save the redirected data gradually, so the nature of the script should bear no result on the outcome. Data should be written in gradually ...

Or (if there is no output at all, even without redirect) you might wanna try fflush(stdout) or something to that effect.

Just a thought (though highly unlikely) ... are you sure redirecting STDOUT is what you ought to do? Isn't it by any chance STDERR? Try running the binary/script/... with:

$ ./program 2> ./stderr_out

and then cat ./stderr_out to make sure STDERRis not the one...

share|improve this answer

Your Answer


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.