在排查WEB系统故障的时候,需要随时关注日志,最希望只要日志有变动,就显示到屏幕上。
但是普通的日志,只能退出vi再重新进,或者不断的使用tail,有没有简便的方法呢?
其实tail本身就可以做这件事情:
man tail
……
-f The -f option causes tail to not stop when end of file is
reached, but rather to wait for additional data to be appended to
the input. The -f option is ignored if the standard input is a
pipe, but not if it is a FIFO.
-F The -F option implies the -f option, but tail will also check to
see if the file being followed has been renamed or rotated. The
file is closed and reopened when tail detects that the filename
being read from has a new inode number.
If the file being followed does not (yet) exist or if it is
removed, tail will keep looking and will display the file from
the beginning if and when it is created.
The -F option is the same as the -f option if reading from stan‐
dard input rather than a file.
……
简单说:
-f 该参数将使tail在遇到文件尾时不退出,而是等待,一旦发现有数据添加到文件,则显示出添加的部分。
-F 同-f,不同的是-F还检查文件是不是被重命名或被滚动。如果tail检测到文件被重建,则重新打开新的文件。
下面示例,开了两个终端,一个启动了tail进行监听aa.txt,另一个对aa.txt进行操作,看两者的表现,为些准备了aa.txt和bb.txt,内容一样:
在左边终端上开启tail,然后在右边终端上添加两行:
删除aa.txt,然后再重建aa.txt:
tail –f就在那儿傻等了,aa.txt已经没有了,它也一直在等待。
用tail –F再试:
添加行的表现差不多。
再看重新aa.txt的表现:
能把新文件的内容显示出来了。
该贴由hui.chen转至本版2014-11-5 17:04:55