2

In RHEL 6 I have the kernel parameter:

printk.time=1

So that my dmesg would have a timestamp. However it shows in seconds from restart, like this:

[12.23456]

I found a script that you place inside .bashrc that converts the seconds to HR date.

dmesg_with_human_timestamps () {
$(type -P dmesg) "$@" | perl -w -e 'use strict;
    my ($uptime) = do { local @ARGV="/proc/uptime";<>}; ($uptime) = ($uptime =~ /^(\d+)\./);
    foreach my $line (<>) {
        printf( ($line=~/^\[\s*(\d+)\.\d+\](.+)/) ? ( "[%s]%s\n",  scalar localtime(time - $uptime + $1), $2 ) : $line )
    }'
}
alias dmesg=dmesg_with_human_timestamps

This works great and my dmesg output now looks like this:

[Fri Jun 10 13:07:14 2016]

But how can I change the code to make that look like this:

[2016/06/10 13:25:28]
5

The POSIX module includes the strftime function, which allows via strftime(3) conversion specifier characters the desired templating of time:

% perl -MPOSIX=strftime -E 'say strftime "[%F %T]", localtime(time)'
[2011-02-17 10:55:37]
%

So in your case make it perl -MPOSIX=strftime -e ... and then

printf( ($line=~/^\[\s*(\d+)\.\d+\](.+)/) ? ( "[%s]%s\n", strftime("%F %T", localtime(time - $uptime + $1)), $2 ) : $line )

You may need the more verbose %Y-%m-%d %H... template if your strftime(3) lacks %F, see strftime(3) for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.