pydmesg: dmesg with human readable timestamps

Since I used dmesg for the first time I felt there was something wrong about it. Having very accurate timestamps might of course be helpful for many people, but sometimes you just want to know when something happened.

dmesg prints timestamps in the form of seconds.nanoseconds since the system booted. And no, there seems to be no -h option to make it human readable.

Today I felt like writing some Python for that, and pydmesg is the result. Is a simple script that fetches the uptime from /proc/uptime and uses it to print nice dmesg timestamps (timestamp format can be changed by editing the file).


[499902.343696] uvcvideo: Failed to query (1) UVC ...
[499902.354633] uvcvideo: Failed to query (1) UVC ...
[530442.358520] npviewer.bin[8818]: segfault at ...


[2010-08-21 13:12:37] uvcvideo: Failed to query (1) UVC ...
[2010-08-21 13:12:37] uvcvideo: Failed to query (1) UVC ...
[2010-08-21 21:41:37] npviewer.bin [8818]: segfault at ...

By default precision is set to the second, which I guess is ok for
human beings ;–)

#!/usr/bin/env python
# coding=utf8
# Copyright (C) 2010 Saúl ibarra Corretgé <>
pydmesg: dmesg with human-readable timestamps
from __future__ import with_statement
import re
import subprocess
import sys
from datetime import datetime, timedelta
_datetime_format = "%Y-%m-%d %H:%M:%S"
_dmesg_line_regex = re.compile("^\[\s*(?P<time>\d+\.\d+)\](?P<line>.*)$")
def exec_process(cmdline, silent, input=None, **kwargs):
"""Execute a subprocess and returns the returncode, stdout buffer and stderr buffer.
Optionally prints stdout and stderr while running."""
sub = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
stdout, stderr = sub.communicate(input=input)
returncode = sub.returncode
if not silent:
except OSError,e:
if e.errno == 2:
raise RuntimeError('"%s" is not present on this system' % cmdline[0])
if returncode != 0:
raise RuntimeError('Got return value %d while executing "%s", stderr output was:\n%s' % (returncode, " ".join(cmdline), stderr.rstrip("\n")))
return stdout
def human_dmesg():
now =
uptime_diff = None
with open('/proc/uptime') as f:
uptime_diff =[0]
except IndexError:
uptime = now - timedelta(seconds=int(uptime_diff.split('.')[0]), microseconds=int(uptime_diff.split('.')[1]))
except IndexError:
dmesg_data = exec_process(['dmesg'], True)
for line in dmesg_data.split('\n'):
if not line:
match = _dmesg_line_regex.match(line)
if match:
seconds = int(match.groupdict().get('time', '').split('.')[0])
nanoseconds = int(match.groupdict().get('time', '').split('.')[1])
microseconds = int(round(nanoseconds * 0.001))
line = match.groupdict().get('line', '')
t = uptime + timedelta(seconds=seconds, microseconds=microseconds)
except IndexError:
print "[%s]%s" % (t.strftime(_datetime_format), line)
if __name__ == '__main__':
view raw pydmesg hosted with ❤ by GitHub

8 thoughts on “pydmesg: dmesg with human readable timestamps

  1. Thx very much for this.BTW I had to make some improvements to this script. Some early lines are displayed with white spaces in time i.e.:[ 820.254259] process `skype’ is using obsolete setsockopt SO_BSDCOMPATSuch lines were not displayed, so I’ve changed _dmesg_line_regex to _dmesg_line_regex = re.compile(“^\[\s*(?P<time>\d+\.\d+)\](?P<line>.*)$”)Now all lines from dmesg have human readable time format.

  2. I came across your article for converting DMESG unix time stamps into human readable format and it worked like a charm! I just cut and pasted your code into a file I created called “pydmesg”, changed permissions on the file to execute and VOILA!Thank you! Now when my SYSLOG’s roll over, and there are previous messages in DMESG, I don’t need to hunt through the SYSLOG files.Thanks again!!Bill

  3. I came across your script while searching for a solution to this, but I also came across a much simpler solution: instead of dmesg, runtail /var/log/kern.logso instead of pydmesg, I use mydmesg defined in my .bashrc:alias mydmesg=’tail -100 /var/log/kern.log’:-)

  4. RDuke: ideed, that could be an optin 🙂 I wrote this after playing a lot with the datetime module, so it was a “just for fun” thing 🙂

  5. This is a really nice program. On debian, or at least on my debian, the kern.log file doesn’t have any information, so your fallback is great..

  6. Nice, but it seems confused by timezone or uptime or something.I had an error around 2012-01-30 14:30 PST of [223281.612941] —[ end trace cc81341889dec48b ]—But your script printed it as [2012-01-29 06:02:31] —[ end trace cc81341889dec48b ]—32 hours earlier. Hmmm. Datetime is hard 😉

Leave a Reply