tail -f file stops tailing on rename

6

I want to tail -f file but going tailing it after a mv.

The tail manpage tells me: -f means --follow=descriptor so a mv should not change the inode?

Why does tail -f stop working after the rename?

Psi

Posted 2014-10-12T17:57:22.263

Reputation: 163

Are you moving across filesystems/partitions? – user1686 – 2014-10-12T18:03:21.230

No, just in the same directory. Tested on Manjaro and Ubuntu 14.04 – Psi – 2014-10-12T18:04:34.293

use tail -F to follow modulo renames, as in http://superuser.com/a/1058674

– timotheecour – 2016-11-16T00:56:12.193

Answers

3

Reading tail sources, it appears that it does not fail at reading the renamed file, but it fails at monitoring the file status.

More precisely, tail appears to behave this (simplified) way:

  1. it reads the file to the end;
  2. then setup an inotify watch in order to be noticed when something happens to the file;
  3. when new content is appended to the file, read again to the end;
  4. then jump back to step 2.

When you move the file, inotify informs tail, which in turn decides to drop the file from the list of the monitored files. It appears to be intentional, although it is not clear to me why this is so (and I would expect it to keep monitoring the file after rename). The relevant lines appear to be these.

So, the problem is not with the underlying Linux operating system, but with the way tail handles file renaming.

Giovanni Mascellani

Posted 2014-10-12T17:57:22.263

Reputation: 511

2I see youre completley right. For me its a bug in tail, either in the code or in the documentation. I use tail wihtin a NodeJS script in wrapped it around with my own inotify watcher to re-init tail using the renamed file, not a elegant way but it works. – Psi – 2014-10-13T10:48:42.880