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:

  1. 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

    ReplyDelete
  2. 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)

    ReplyDelete
  3. Thanks J!

    This is perfect:

    watch "dmesg | tail -20"

    ReplyDelete
  4. 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

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

    ReplyDelete
  6. 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.

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

    ReplyDelete