1

Let's imagine this situation:

A file is being continuously appended by text in a while true loop with no sleep between iterations.

while true
do
echo "foo" >> source.file
done 

Another process is started which does cp or rsync of the file to another location. I noticed that the file is copied with specific size(I guess the size the source file was at the moment the copying process was started) and then the copying process ends.

How does it know when to stop copying since the size of the file is continuously increasing during the copy process is running?

1 Answer 1

1

This happens when you copy a file which is only 27 bytes large (strace cp a b):

read(3, "Who\222s there?\nIt's just me!\n", 131072) = 27
write(4, "Who\222s there?\nIt's just me!\n", 27) = 27
read(3, "", 131072)                     = 0

Although cp knows the size (and it does not change during the copying) cp tries to read up to 128K of data. It stops copying if a read() delivers zero bytes. That is the sign that the end of the file is reached.

If the copy process would read data more slowly than it is written (e.g. due to different I/O priorities or other I/O limits like cgroups controllers) then the copying would "never" stop.

2
  • Thanks for answer. Btw, do you know why lsof doesn't show the source.file(from the question) while the writing process is running?
    – CuriousGuy
    Mar 31, 2018 at 18:36
  • @CuriousGuy I cannot reproduce that. Of course, an echo would be too short-lived to show up in lsof but if you do cat >>file in one shell and lsof in another then you see it. Apr 1, 2018 at 21:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .