Linux 实时查看日志文件的 4 种方法,总有一款适合你
来自:高效运维
在 Linux 下如何才能实时查看日志内容呢?有很多工具可以帮助我们在文件持续修改的同时输出文件内容,最常用的莫过于 tail 命令了。
1. tail Command – Monitor Logs in Real Time
如前所述,tail 命令是实时显示日志的最常用的方法。不过该命令有两个版本,如下所示。
第一个例子是为 tail 命令加上
-f
参数。$ sudo tail -f /var/log/apache2/access.log
第二个例子就是
tailf
命令。它本身内建了 -f
参数,因此你无需再为它指定 -f
参数。$ sudo tailf /var/log/apache2/access.log
通常 Linux 服务器上的日志都是轮转日志。这种情况下,你需要用
-F
参数。tail -F 会监控是否创建了新日志(所谓新日志指的是同一个名字,但是 fd 不一样的日志文件),并且会转而显示新日志的内容,而不是老文件的内容。
$ sudo tail -F /var/log/apache2/access.log
然而,默认情况下 tail 命令只会显示文件最后 10 行的内容。如果你只想在实时模式下查看最后两行的内容,那么可以连用
-n
和 -f
参数,如下这样:$ sudo tail -n2 -f /var/log/apache2/access.log
2. Multitail Command – Monitor Multiple Log Files in Real Time
另一个好玩的命令是 Multitail Command。从名字中就能看出它可以实时监控多个日志,Multitail 还能让你前后翻阅被监控的文件。
使用下面命令可以在基于 Debian 或 RedHat 的系统中安装 Mulitail。
$ sudo apt install multitail [On Debian & Ubuntu]
$ sudo yum install multitail [On RedHat & CentOS]
$ sudo dnf install multitail [On Fedora 22+ version]
下面例子演示了如何同时显示两个日志文件。
$ sudo multitail /var/log/apache2/access.log /var/log/apache2/error.log
3. lnav Command – Monitor Multiple Log Files in Real Time
另一个类似 Multitail 的命令是 Lnav,Lnav 也能实时监控多个文件。
使用下面命令可以在基于 Debian 或 RedHat 的系统中安装 Lnav。
$ sudo apt install lnav [On Debian & Ubuntu]
$ sudo yum install lnav [On RedHat & CentOS]
$ sudo dnf install lnav [On Fedora 22+ version]
使用 Lnav 同时查看两个日志的方法如下:
$ sudo lnav /var/log/apache2/access.log /var/log/apache2/error.log
4. less Command – Display Real Time Output of Log Files
最后你可以用 less 命令 查看日志文件,然后按下
Shift+F
也能实时查看日志内容。跟
tail
一样, 在 less 中按下 Shift+F 会追踪文件末尾的内容。你也可以在调用 less 命令时就加上 +F 参数。sudo less + F /var/log/apache2/access.log
下面是其他一些关于日志监控与管理的文章,值得一读。
Manage Files Effectively using head, tail and cat Commands in Linux How to Setup and Manage Log Rotation Using Logrotate in Linux Petiti – An Open Source Log Analysis Tool for Linux SysAdmins How to Query Audit Logs Using ‘ausearch’ Tool on CentOS/RHEL Manage Log Messages Under Systemd Using Journalctl {Comprehensive Guide}
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604241
Linux技术交流群:3586725
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。