redirected output is buffered, how do you flush it?
Tom Christiansen
tchrist at convex.COM
Tue Feb 5 11:06:29 AEST 1991
>From the keyboard of rhoward at msd.gatech.edu (Robert L. Howard):
:I have a script that after several pipe stops outputs a line
:of information. When run from a tty it outputs a line every
:few seconds to several minutes. Wrapped around the commands
:is an infinite loop. I can specify a trap command that will
:close up things gracefully (print summary info etc.).
:
:The problem comes in when I run:
:
:% script > some_file
:
:and then kill it some number of minutes later. The total output
:of the script is still in some buffer somewhere and doesn't make
:it to the file. Is there some command I can put in the 'trap' to
:force it to flush the buffers? Or is there a recommended way to
:kill the job (other than ^C) that will force the buffers to flush?
This is a general problem that comes up often, and I don't know any
way of doing it unless you can get the program doing the writes to
flush its buffers now and then. If awk only had a way to force flushes,
then it wouldn't be so rough; sadly, it doesn't.
:Here is the script if you are wondering what I am talking about...
:
:------------------------------------------------------------------
:#! /bin/sh
:#
:pstat=/usr/etc/pstat
:
:
:trap <Some-command_here> 1 2 3 14 15
:
:echo "Starting at `date`"
:echo "Interval is $1 seconds."
:echo ""
:
:while true
:do
: $pstat -T | sed -e 's/\//\ /g'
: sleep $1
:done | awk '
:/files/ { if ($1 > files) {
: files = $1
: printf ("max files\t%5d out of %5d, or %6.2f%\n", \
: files, $2, 100*files/$2) }
: }' -
Here's a fairly direct translation of your program into perl,
which does have a way to set buffering: if $| is non-zero, then
output will be flushed after each print. It even looks a lot
like your script.
#!/usr/bin/perl
$pstat='/usr/etc/pstat';
$nap = shift; # i like to name my args
$| = 1; # auto-flush at end of print statements
print "Starting at ", `date`;
print "Interval is $nap seconds.\n\n";
while (1) {
`$pstat -T` =~ /(\d+)\/(\d+) files/; # $1 and $2 get set here
if ($1 > $files) {
$files = $1;
printf ("max files\t%5d out of %5d, or %6.2f%%\n",
$files, $2, 100*$files/$2);
}
sleep $nap;
}
--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI
window manager, which argues with you 10 weeks before resizing your window."
### And now for the question of the month: How do you spell relief? Answer:
U=brnstnd at kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U
More information about the Comp.unix.questions
mailing list