2021-01-09:linux中,某一个实时日志通过什么命令查?

福哥答案2020-01-09:
[答案来自此链接:](https://www.zhihu.com/question/438536200)
1.tail
tail -f
首先就是 tail -f,tail 命令是实时显示日志文件的最常用解决方案,加上 -f 参数可以实时拉取日志最新的更新:
tail -f /var/log/access.log
同时,默认是查看最后 10 行,可以加上行数增加查看的最后的行数,例如查看最后 100 行:
tail -00f /var/log/access.log
并且,tail 可以使用匹配符匹配多个文件,例如:
tail -100f /var/log/access*.log
如果你的日志会滚动,那么可以使用 -F 参数,这样会在文件滚动之后追踪新文件,而不是老的文件。
tail -F /var/log/access.log。
底层原理是:
tail.c
/* Tail N_FILES files forever, or until killed.
The pertinent information for each file is stored in an entry of F.
Loop over each of them, doing an fstat to see if they have changed size,
and an occasional open/fstat to see if any dev/ino pair has changed.
If none of them have changed size in one iteration, sleep for a
while and try again. Continue until the user interrupts us. */
static void tail_forever (struct File_spec *f, size_t n_files, double sleep_interval)
{
}
简而言之就是先将匹配到的文件加入到数组中,定时遍历这个数组,利用fstat函数检查每个文件的大小变化,如果有变化,就读取上次记录的大小到当前记录的大小的文件内容,并输出。注意,如果文件变化的越快(但是也不用太担心,硬盘写入没那么快,不可能超过内存写入速度),占用的内存越多。

2.less命令
less 命令之后键入 Shift+F,这样可以实时追踪最新更新。但是,这样会把文件内容的大部分载入内存中,如果日志文件很大,会吃掉很多内存,不推荐这种做法。
***
[评论](https://user.qzone.qq.com/3182319461/blog/1610148967)

原文地址:https://www.cnblogs.com/waitmoon/p/14258829.html