Pages - Menu

Monday, May 25, 2009

how to monitor dmesg in real time

I was looking around for a way of monitoring dmesg output in realtime.

Some distros allow you to
tail -f /var/log/dmesg
however this file stops being written in Ubuntu after boot, so that's not so useful.

I then found the watch command, which executes a command every 2 seconds (configurable if you like). Just watching dmesg fills my terminal output from the start, which is less than useful, so I use tail to shrink it down a bit. The command:
watch "dmesg | tail -20"
does exactly what I was after.

8 comments:

Hieu Le Trung said...

I think you can create following script
#!/bin/bash
while [ 1 ]; do
dmesg -c
done

And run with
./dmesg_mon >> dmesg_log

Then the dmesg will be log into file instead of console ;)

Cheers,
-Hieu

Matt Kukowski said...

If you want to watch your kernel messages (dmesg) than tail -f kern.log

Now you can tail and log all at the same time ( this is on Ubuntu which is what you said you were using)

David K. said...

Thanks J!

This is perfect:

watch "dmesg | tail -20"

Daniel d'Andrada T. de Carvalho said...

Super useful. Thanks!

Typhon said...

Hi
I needed a complete dmesg log but avoid to delete it with dmesg -c and ended up with the "one-liner"

export F=/tmp/dmesg.log ; \
rm -f $F ; \
while true; do \
dmesg|comm -1 -3 --nocheck-order $F - | tee -a $F ; \
done

Anonymous said...

use "dmesg -wH" - it will print out the last messages in the dmesg ring buffer in real time

Jamie said...

Nice tip! "dmesg -wH" does require kernel 3.5 or later and a new util-linux version. This blog post was written long before either of those existed. Even current Ubuntu LTS's util-linux is not new enough for it.

Firo Yang said...

Another simple way:
Using 'Ctrl + Alt + Fn' to switch virtual console.