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)
Max*_*xim 72
在 Linux 上,从内核内核 3.5.0 开始,您可以使用:
dmesg -w
Run Code Online (Sandbox Code Playgroud)
同样在systemd您可以使用的系统上:
journalctl -kf
Run Code Online (Sandbox Code Playgroud)
dje*_*kyb 56
man dmesg)/proc/kmsg(请参阅参考资料man proc)/proc/kmsg直接读取,即cat /proc/kmsg。现在,如果您阅读了友好的 proc 手册,它会严厉警告您一次只能让一个用户(必须有特权)阅读/proc/kmsg。无论您拥有什么 syslog 实现,都应该这样做,并且大概它适用于dmesg. 我不知道,我在这里不合时宜,只是解释一下手册。因此,虽然这是“只是让它@#$%ing 工作”的方式,但首先考虑接下来的几个方法。
在我与 systemd init* 一起使用的一个 linux 机器上,dmesg.log 不经常写入,也许根本不写入?我发现连续读取内核日志缓冲区的最佳方法是使用watch. 像这样的事情应该让你开始(调整你的终端中有多少行):
watch 'dmesg | tail -50'
Run Code Online (Sandbox Code Playgroud)
更复杂的解决方案可能会使用 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 行)
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”。
小智 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)