Chinaunix

标题: 关于tail的问题请教大家。 [打印本页]

作者: hexyldog    时间: 2008-01-11 14:58
标题: 关于tail的问题请教大家。
uuu@localhost:~/test$ touch test.log
uuu@localhost:~/test$ vi test.log

写入 1234 存盘退出

另开一终端uuu@localhost:~/test$tail -f test.log

此时显示的是1234


然后

uuu@localhost:~/test$: mv test.log test.log.1
uuu@localhost:~/test$ touch test.log
uuu@localhost:~/test$ vi test.log
写入 abcd 存盘退出
uuu@localhost:~/test$ ll
total 8
-rw-r--r-- 1 uuu root 15 2008-01-11 14:47 test.log
-rw-r--r-- 1 uuu root 29 2008-01-11 14:47 test.log.1

此时切换到tail窗口,发现内容并没有变化。为什么呢?

需要C-c 在重新tail -f  test.log 才能看到期望的 abcd

[ 本帖最后由 hexyldog 于 2008-1-11 15:01 编辑 ]
作者: lb_bn    时间: 2008-01-11 15:38
tail不是实时的。。
作者: yingweixu    时间: 2008-01-11 15:42
当你执行 uuu@localhost:~/test$: mv test.log test.log.1 这个语句时,另一个终端的界面实际上已经被终止了!
作者: yingweixu    时间: 2008-01-11 15:48
确实tail -f 在查看挂在后台程序所输出结果的时候是实时的。比如看日志文件的时候。
但是你可以试看看:你创建一个文件,然后一个终端tail来看这个文件,然后用另外一个终端来修改他里面的内容时,用tail查看的终端并没有实时的刷新文件。
希望有哪个高手能帮忙解答看看 tail 何时是属于实时刷新内容的!
作者: hexyldog    时间: 2008-01-11 17:12
标题: 回复 #4 yingweixu 的帖子
多谢两位关注。问题解决了

默认是根据 file descriptor检索的。

-F 参数可以解决

tail --help

-F                       same as --follow=name --retry

With  --follow  (-f), tail defaults to following the file descriptor, which means that even if a tail'ed file is renamed, tail will
       continue to track its end.  This default behavior is not desirable when you really want to track the actual name of the  file,  not
       the  file  descriptor (e.g., log rotation).  Use --follow=name in that case.  That causes tail to track the named file by reopening
       it periodically to see if it has been removed and recreated by some other program.
作者: 志国    时间: 2008-01-11 17:19
原帖由 hexyldog 于 2008-1-11 17:12 发表
多谢两位关注。问题解决了

默认是根据 file descriptor检索的。

-F 参数可以解决


tail --help

-F                       same as --follow=name --retry

With  --follow  (-f), tail defaults  ...



不错!
作者: int3    时间: 2008-01-11 17:21
个人认为 i 节点变化了!
作者: hexyldog    时间: 2008-01-11 17:42
原帖由 int3 于 2008-1-11 17:21 发表
个人认为 i 节点变化了!


请问一下

inode 就是指 file descriptor 吗?

那我用-F参数是否会带来其他问题啊?

目前看没什么问题,基本实现了我要的功能。我现在很担心会不会带来什么副作用。
作者: maxxfire    时间: 2008-01-13 09:57
tail -F 也不是万能的,有一个问题:
比如在tail -F A 时, 若此时读到第10行,这个时候你将一个超过10行的文件B替换A: mv B A
那么,就直接从文件B的第11行开始读,跳过文件B的前10行,而不是从新文件的开头开始读。
作者: demon64    时间: 2008-01-14 09:37
收下了,感谢ing~




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2