How to Understand file writing in Progress in UNIX?

Hi All,

I have a requirement, where i have to pool file A data to file B continuously and need to process the data in the file B.

Since the data need to be processed only once so i have to truncate the data in file A after every pool. So that on the next pooling i can get the fresh data and there will be no duplication.

Issue: In case when i am truncating the data of FileA if some date getting written on the FileA then it will also get truncated. So i am looking for a process where i can capture that the file is getting written or some way by which i can lock the file for truncation.

My piece of sample code:

n=1
while ($n=1)
do
    Input_File= FileA.txt
    Copy_File= FileB.txt
    cp $Input_File $Copy_File
    >$Input_File 
    #Post above steps i process the data in FileB.txt with my filthy code
done

Thanks for you Suggestion in Advance

What shell are you using? There are some errors in your code to start with.

Does a writer process keep fileA open or does its inode need to remain constant? If not, why don't you just mv the file?

Maybe it is as simple as

tail -f file.a >fileb