Do something like this once, after opening the output filehandle:
in reply to Buffering File Output
select(OUTPUT); # Change the default filehandle
$|=1; # Turn on autoflushing for default f.h.
select(STDOUT); # Go back to normal default f.h.
When processes exit normally (via the exit function or simply falling off the end of the script) pending output on filehandles is flushed and the filehandles are closed. What's curious is that this isn't happening for you. Are you terminating the script in some odd way? Segfaulting it? Killing it with a signal?