我尝试显示文本文件的尾部。如果文件很小,则没有区别。但是,如果文件太大(〜5 gB),tailf将不会响应。另一方面,tail -f可以正常工作。它们之间有什么区别?
我也遇到过同样的问题。日志文件约为47GB。的tailf
只是等待几乎是无限的。但是tail -f
开始在几秒钟内打印输出。
通过使用strace
命令检查底层系统调用,我进行了更深入的研究。结果如下:
# strace tailf /var/log/messages
(truncated)
stat("/var/log/messages", {st_mode=S_IFREG|0600, st_size=47432599401, ...}) = 0
open("/var/log/messages", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0600, st_size=47432600425, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7dba2d1000
read(3, "Nov 1 03:23:01 hostnameXXXX"..., 4096) = 4096
read(3, "0.31.148.12)\nNov 1 03:54:33 del"..., 4096) = 4096
read(3, "io.c(600) [receiver=3.0.6]\nNov "..., 4096) = 4096
(truncated)
Run Code Online (Sandbox Code Playgroud)
如您所见,在tailf
尝试将所有行从开始读取(缓冲),然后生成输出到屏幕。
检查tail -f
下面的输出,这里使用系统调用lseek
(C / C ++)直接跳转到文件末尾并从那里开始读取:
# strace tail -f /var/log/messages
(truncated)
open("/var/log/messages", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0600, st_size=47294167448, ...}) = 0
lseek(3, 0, SEEK_CUR) = 0
lseek(3, 0, SEEK_END) = 47294170917
lseek(3, 47294169088, SEEK_SET) = 47294169088
(truncated)
Run Code Online (Sandbox Code Playgroud)
从手册页:
tailf will print out the last 10 lines of a file and then wait for the
file to grow. It is similar to tail -f but does not access the file
when it is not growing. This has the side effect of not updating the
access time for the file, so a filesystem flush does not occur periodi-
cally when no log activity is happening.
Run Code Online (Sandbox Code Playgroud)
http://linuxcommand.org/man_pages/tailf1.html
如果它不直接访问文件,那么处理非常大的文件时会遇到一些困难,具体取决于您的计算机设置。