[ALUG] Standard output redirection caching

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Fri Jun 1 11:53:05 BST 2007

On 01-Jun-07 10:04:52, Dan Hatton wrote:
> Dear All,
> When standard output is redirected to a file, it gets cached, so that
> nothing is actually written to the file until (say) 4kB of standard
> output has built up.  I'm sometimes too impatient to wait that long to
> see the output of my programs, especially when the program in
> question is a long, slow numerical calculation that might be producing
> only a few hundred bytes of output per day.  Is there any way of
> either switching off this caching, or inspecting the contents of the
> cache before it gets written, please?
> -- 
> Thanks very much
> Dan Hatton

I don't know about turning off caching. And, even if it's possible,
it would probably apply globally (not just to a specific application),
and that would not be a good idea.

I don't think it's possible to inspect the cache, though could be
wrong about that.

However, there's one thing you can do to get caches out to the
files they belong to: use the 'sync' command.

  SYNC(1)                   FSF                   SYNC(1)

    sync - flush filesystem buffers

    sync [OPTION]

    Force changed blocks to disk, update the super block.

Despite the synopsis, 'sync' has no useful options (only "--help"
and "--version").

Again, this is global, so you don't want to do it at frequent
intervals, since it can take a second or few to commplete.

So, if your program produces output slowly as you describe,
and you want to inspect it from time to time, you can either
run a script in the background on the lines of

while true ; do
sleep 30m

or from a separate xterm/console simply enter "sync" whenever
you feel the urge.

Either way, the accumulated output at the latest invovation
of 'sync' can then be inspected in the destination file.
Indeed, if you use the script method above, or even if you
do it manually, it may be useful to have a separate xterm
in which you are running

  tail -f myoutputfile

so that any new changes will immediately appear in this xterm.

By the way, you don't need to be root to use 'sync', though it
does no harm if you are.

Hoping this helps,

E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-Jun-07                                       Time: 11:52:44
------------------------------ XFMail ------------------------------

More information about the main mailing list