How can I see dmesg output as it changes?
I’m writing a device driver that prints error message into ring buffer dmesg output.
I want to see the output of dmesg
as it changes.
How can I do this?
You can’t really monitor the output of dmesg
directly.
However, chances are high that your module is not printing directly into the ring-buffer of dmesg, but instead uses the kernel logging facilities (which will then be displayed by dmesg
). If your syslog
has some sane (e.g. default) settings, these messages will most likely also show up in the kern.log
logfile.
So you can do something like:
tail -f /var/log/kern.log
You can use the watch
command which is intended exactly for things like this
watch -n 0.1 "dmesg | tail -n $((LINES-6))"
the $((LINES-6))
part should make it fit nicely into your terminal.
You use dmesg
to get log messages of the kernel.
The kernel itself logs into a ring buffer, i.e. just in memory.
Now all dmesg
does is output the content of that ring buffer.
If you do dmesg -c
it will also delete the ring buffer afterwards.
Therefore you could do something like while true; do dmesg -c; sleep 1; done
to have something like the equivalent of a not working dmesg|tail
. But this deletes the ring buffer and therefore needs root powers.
The other way is the file /proc/kmsg
which allows a view on the ring buffer. You could do tail -f /proc/kmsg
, but this is only allow to one process, and this is usually your logging daemon. – It’s job is to read the messages and write it to real files (usually in /var/log) where they can be read. It can be configured to output all messages to a single file or different parts into different files. (But configuration depends on the logging daemon of your system.)
Therefore have a look at /var/log
if there is any file which suits your needs and configure your logging daemon otherwise.
Relatively recent dmesg
versions provide a follow option (-w
, --follow
) which works analogously to tail -f
.
Thus, just use following command:
$ dmesg -wH
(-H
, --human
enables user-friendly features like colors, relative time)
Those options are available for example in Fedora 19.
On systems that use systemd
you can also:
# journalctl -kf
Use these two commands from separate terminals:
while true; do dmesg -c >> test.txt;sleep 1; done
tail -f test.txt
It will achieve a similar result.
If you are using an embedded system, like BusyBox that is common on systems like OpenWrt, it has very limited functionality and only 2-3 flags are supported.
If you want a quick and dirty way of printing dmesg output on the screen continually as events change, a simple Bash loop works fine. It’s not ideal, but as I mentioned the BusyBox dmesg is missing a lot of functions. I find the following has the same effect when entered in to the command line:
$ while true; do dmesg -c ; sleep 1 ; done
You can quit the loop with Ctrl + C. The sleep 1 is to stop it battering the CPU needlessly, and the -c flag clears the buffer on each call so you don’t see repeated output every second.