How do I convert dmesg timestamp to custom date format?

前端 未结 9 1255
感动是毒
感动是毒 2020-12-12 17:41

I am trying to understand the dmesg timestamp and find it hard to convert that to change it to java date/custom date format.

Sample dmesg log:

<         


        
9条回答
  •  被撕碎了的回忆
    2020-12-12 18:17

    A caveat which the other answers don't seem to mention is that the time which is shown by dmesg doesn't take into account any sleep/suspend time. So there are cases where the usual answer of using dmesg -T doesn't work, and shows a completely wrong time.

    A workaround for such situations is to write something to the kernel log at a known time and then use that entry as a reference to calculate the other times. Obviously, it will only work for times after the last suspend.

    So to display the correct time for recent entries on machines which may have been suspended since their last boot, use something like this from my other answer here:

    # write current time to kernel ring buffer so it appears in dmesg output
    echo "timecheck: $(date +%s) = $(date +%F_%T)" | sudo tee /dev/kmsg
    
    # use our "timecheck" entry to get the difference
    # between the dmesg timestamp and real time
    offset=$(dmesg | grep timecheck | tail -1 \
    | perl -nle '($t1,$t2)=/^.(\d+)\S+ timecheck: (\d+)/; print $t2-$t1')
    
    # pipe dmesg output through a Perl snippet to
    # convert it's timestamp to correct readable times
    dmesg | tail \
    | perl -pe 'BEGIN{$offset=shift} s/^\[(\d+)\S+/localtime($1+$offset)/e' $offset
    

提交回复
热议问题