是否可以“tail -f”输出“dmesg”?

Iva*_*iao 173 bash command-line tail

我想做类似的事情

dmesg | tail -f
Run Code Online (Sandbox Code Playgroud)

但它不起作用:

我使用 Mac OS X v10.6.7 (Snow Leopard)。通过这样做,tail将退出,而不是监视输出。

我想知道是否有办法做到这一点,或等效的命令。

PS,我认为while循环不是一个好主意。

Cal*_*leb 165

您可能正在寻找来自各种日志文件的消息的某种组合。尝试:

tail -f /var/log/{messages,kernel,dmesg,syslog}
Run Code Online (Sandbox Code Playgroud)

...获得对系统的一个很好的概述。如果您想要更多或更少,请研究您想要查看的消息被放置在哪个日志文件中。

还要考虑使用multitail文件和颜色代码并一次过滤多个日志文件。

编辑:当我回答这个问题时,这不是很相关,但是由于这个页面获得了很多点击,我认为值得一提的是运行 systemd 的较新系统有这个。

dmesg -w
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的提醒:`multitail`。看起来很有趣。在 OS X 的情况下,它将类似于:`tail -f /var/log/{system.log,kernel.log}`。 (6认同)
  • `system.log` 和 `kernel.log` 不包含 OS X 上 `dmesg` 的确切输出。例如,对于损坏的驱动器,`dmesg` 中的文件读取错误准确指定了无法读取的文件,而不幸的是,`kernel.log` 只提供了不太有用的通知:`disk0s2:I/O 错误。` (3认同)
  • 作为记录,此答案不适用于 OS X Mavericks (10.9) 或 Arch Linux。 (3认同)
  • ++ 在编辑上。 (3认同)

Max*_*xim 72

在 Linux 上,从内核内核 3.5.0 开始,您可以使用:

dmesg -w
Run Code Online (Sandbox Code Playgroud)

同样在systemd您可以使用的系统上:

journalctl -kf
Run Code Online (Sandbox Code Playgroud)

  • `dmesg -w` 绝对是最好的解决方案。不幸的是,即使是 Ubuntu 14.04 似乎也没有为此做好准备,因为用户空间工具还不支持它。 (6认同)
  • 是的,这是一个不错的小金块。可以通过以下方式使人类可读: dmesg -wH (2认同)

dje*_*kyb 56

让它@#$%ing 工作

  1. 您想不断地、立即地打印 dmesg 的输出
  2. Dmesg 正在打印内核环形缓冲区(请参阅 参考资料man dmesg
  3. 内核环形缓冲区是一个特殊的 proc 文件,/proc/kmsg(请参阅参考资料man proc
  4. /proc/kmsg直接读取,即cat /proc/kmsg

现在,如果您阅读了友好的 proc 手册,它会严厉警告您一次只能让一个用户(必须有特权)阅读/proc/kmsg。无论您拥有什么 syslog 实现,都应该这样做,并且大概它适用于dmesg. 我不知道,我在这里不合时宜,只是解释一下手册。因此,虽然这是“只是让它@#$%ing 工作”的方式,但首先考虑接下来的几个方法。

批准的手册页:watch + dmesg

在我与 systemd init* 一起使用的一个 linux 机器上,dmesg.log 不经常写入,也许根本不写入?我发现连续读取内核日志缓冲区的最佳方法是使用watch. 像这样的事情应该让你开始(调整你的终端中有多少行):

watch 'dmesg | tail -50'
Run Code Online (Sandbox Code Playgroud)

手表 + dmesg + 守护进程 + 尾 -f

更复杂的解决方案可能会使用 watch 将 dmesg 输出写入文件,然后您可以tail -f. 您可能希望它作为守护程序运行。一个适当的守护进程也将 gzip 和旋转日志。以下 bash 代码未经测试,无法正常工作,仅用于传达一个想法。@Brooks Moses 的回答有一个工作版本

watch 'dmesg >> /var/log/dmesg.log | tail -1'
Run Code Online (Sandbox Code Playgroud)

*切线,因为这是一个关于苹果桌面操作系统的问题:当systemd在身边时,不要打扰dmesg; 使用journalctl -xf(也许-n 100还可以显示前 100 行)

  • 看起来它直接在内核内存中。Apple 的 `dmesg` 实现的源代码:http://www.opensource.apple.com/source/system_cmds/system_cmds-230.7/dmesg.tproj/dmesg.c Quick Googling 没有提到任何关于它在文件系统中表示的内容:/ (2认同)

Bro*_*ses 21

这是实际测试过的djeikyb 答案的一个变体,并修复了几个错误。

watch 'sudo dmesg -c >> /tmp/dmesg.log; tail -n 40 /tmp/dmesg.log'
Run Code Online (Sandbox Code Playgroud)

The important trick is that we're doing dmesg -c, which clears the ring buffer after it prints -- thus, each time through we're only printing what's new since the last time.

You'll need to be root to do that, thus the sudo. There's also a bugfix; instead of trying to both dump the output to a file and pipe it to tail (which doesn't work), we're just reading from the newly-written file.

We could do just dmesg > /tmp/dmesg.log and overwrite the whole file each iteration, but that's a lot of I/O and also risks losing the file if the computer crashes in the middle of an overwrite.

You could also do something similar that's more closely like tail -f with a while loop that executes dmesg -c and sleep 1 forever (see Ben Harris's answer). However, since this is actually clearing the kernel message buffer as it's running, you may also want to pipe things into a logfile in case you want them later.


小智 7

这可能对你有用

while true;do sudo dmesg -c;done
Run Code Online (Sandbox Code Playgroud)

请记住,'-c' 标志会将消息缓冲区清除到标准输出中。如果您是 root,则不需要 'sudo'。如果您觉得这占用了过多的 CPU 资源,请尝试在循环完成之前添加一个“睡眠 1”。

  • 随意引用您的来源:http://www.linuxforums.org/forum/applications/36555-tail-dmesg.html#post730211 (3认同)
  • 又快又脏。肮脏,因为它只有在您是唯一一个这样做的用户时才有效。否则每个用户只能收到一半的消息 (2认同)

小智 5

在看到这篇文章之前,我是这样做的:

#!/usr/bin/env perl

use strict;
use warnings;

# "tail -f" for dmesg
# Keeps last printed line. Anything sorting "gt" will be newer

$|=1;

my $y = '';

while(1) {
    for my $k (`dmesg`) {
        if ($k gt $y) {
            print $k;
            $y = $k;
        }
    }
    sleep 1;
}
exit;
Run Code Online (Sandbox Code Playgroud)