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?
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)
NAME sync - flush filesystem buffers
SYNOPSIS sync [OPTION]
DESCRIPTION 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 sync sleep 30m done
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, Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 01-Jun-07 Time: 11:52:44 ------------------------------ XFMail ------------------------------
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 just realised that I inadvertently, in my previous response, answered your "Is there any way of ... inspecting the contents of the cache before it gets written?" in the affirmative!
Namely, as I put in as an afterthought, use "tail -f". Example:
Have 2 xterms going. In xterm 1:
echo `date` > myoutfile
(note backquotes). In xterm 2:
tail -f myoutfile
and see one line like "Fri Jun 1 12:38:45 BST 2007". Now, in xterm 1:
while true ; do echo `date` >> myoutfile ; sleep 2 ; done
and watch the output accumulate in xterm 2, every 2 seconds (i.e. you're seeing it before the cache gets flushed). So far:
Fri Jun 1 12:38:45 BST 2007 Fri Jun 1 12:40:31 BST 2007 Fri Jun 1 12:40:33 BST 2007 Fri Jun 1 12:40:35 BST 2007 Fri Jun 1 12:40:37 BST 2007 Fri Jun 1 12:40:39 BST 2007 Fri Jun 1 12:40:41 BST 2007 Fri Jun 1 12:40:43 BST 2007 Fri Jun 1 12:40:45 BST 2007 Fri Jun 1 12:40:47 BST 2007 Fri Jun 1 12:40:49 BST 2007 Fri Jun 1 12:40:51 BST 2007 Fri Jun 1 12:40:53 BST 2007 Fri Jun 1 12:40:55 BST 2007 Fri Jun 1 12:40:57 BST 2007 Fri Jun 1 12:41:00 BST 2007 Fri Jun 1 12:41:02 BST 2007 Fri Jun 1 12:41:04 BST 2007 Fri Jun 1 12:41:06 BST 2007 Fri Jun 1 12:41:08 BST 2007 Fri Jun 1 12:41:10 BST 2007 Fri Jun 1 12:41:12 BST 2007
Kill either program with ^C when desired.
Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 01-Jun-07 Time: 12:42:08 ------------------------------ XFMail ------------------------------