I'm trying to figure out under what conditions a text file would not be readable on the filesystem while a perl script is writing to that file.

Here is some test code:
=================================== #!/usr/bin/perl -w use strict; print "Begin...\n"; for ( my $i = 0; $i < 5; $i++ ) { print "$i...\n"; sleep 2; } ===================================

I execute this script like this:
$ ./populate.pl > data.txt&


So I background it, because I expect that the script will continue writing while I execute this next command:

$ tail -f data.txt


Now 'tail' just sits there, and doesn't output anything until the populate.pl script is complete.

~HOWEVER~

If I do this with a straight bash script, as follows:
while [ 1 ]; do echo "some text..."; done > data.txt&
..and then do a 'tail -f' on the data.txt file, I can see it's content while the bash script is writing to it.
In both cases I am using '>' to continuously write, and not '>>' to append.

I currently do have other perl scripts which are executed as './script.pl > output.txt', and I am able to do a 'tail' on the output file while the script is running, without any problems.

But the first script above doesn't allow me to tail the data.txt file, so I'm trying to understand the difference, or what would prevent the 'tail' command from working in the first scenario. Anyone have any ideas?

(Please note, I know there are many other ways to accomplish reading from a file while it is being written to; this is just an exercise to increase my understanding of what exactly is going on in this particular situation :)

Bobby

In reply to Redirecting perl output on the command line by jbl_bomin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.