linux使用dmesg设置易读的时间格式

发布时间 2023-09-04 11:26:23作者: 风行天下-2080

1、

http://www.taodudu.cc/news/show-316388.html?action=onClick

网卡或者硬盘有问题时,我们可以通过使用dmessage命令查看相关信息

直接用dmesg显示系统信息,很难看

[95721.670025] snapshot device recevied [read] io request, access on dev sector[272], length is [240] sectors.
[95721.670363] device is closed
[95721.693207] device is closed
显示的时间没有办法看。

使用dmesg --help看到,如下:

Usage:
 dmesg [options]

Display or control the kernel ring buffer.

Options:
 -C, --clear                 clear the kernel ring buffer
 -c, --read-clear            read and clear all messages
 -D, --console-off           disable printing messages to console
 -E, --console-on            enable printing messages to console
 -F, --file <file>           use the file instead of the kernel log buffer
 -f, --facility <list>       restrict output to defined facilities
 -H, --human                 human readable output
 -k, --kernel                display kernel messages
 -L, --color[=<when>]        colorize messages (auto, always or never)
                               colors are enabled by default
 -l, --level <list>          restrict output to defined levels
 -n, --console-level <level> set level of messages printed to console
 -P, --nopager               do not pipe output into a pager
 -r, --raw                   print the raw message buffer
 -S, --syslog                force to use syslog(2) rather than /dev/kmsg
 -s, --buffer-size <size>    buffer size to query the kernel ring buffer
 -u, --userspace             display userspace messages
 -w, --follow                wait for new messages
 -x, --decode                decode facility and level to readable string
 -d, --show-delta            show time delta between printed messages
 -e, --reltime               show local time and time delta in readable format
 -T, --ctime                 show human readable timestamp (may be inaccurate!)
 -t, --notime                don't print messages timestamp
     --time-format <format>  show time stamp using format:
                               [delta|reltime|ctime|notime|iso]
Suspending/resume will make ctime and iso timestamps inaccurate.

 -h, --help     display this help and exit

可以使用-T来显示可读时间,如下:

dmesg -T

[Fri Aug 31 17:55:32 2018] device is closed
[Fri Aug 31 17:55:32 2018] device is closed

二、

https://blog.csdn.net/a1809032425/article/details/129345486

centos7 下可以直接用 dmesg -T 命令将时间戳输出为可读格式, unbuntu 下需要下面的操作:

1.获取dmesg的时间戳
# dmesg
[   18.204448] Bluetooth: RFCOMM socket layer initialized
[   18.204452] Bluetooth: RFCOMM ver 1.11
2.时间戳表示的含义
dmesg 日志中的“时间”(18.204452) 表示的是系统启动到事件发生的时间差,这个值可以转换成时间戳:

unix_time=`echo "$(date +%s) - $(cat /proc/uptime | cut -f 1 -d' ') + 18.204452 " | bc`
echo ${unix_time}
注:/proc/uptime 第一列表示的是系统开机时间,根据这个值和当前时间可以获取到 dmesg 日志中事件发生的时间

3.把时间戳转换为对应的可读的时间
 date -d "@${unix_time}" '+%Y-%m-%d %H:%M:%S'
4.脚本文件如下
#!/bin/bash
 
if [ $# -ne 1 ];then
    echo "input an dmesg time"
    exit 1
fi
 
unix_time=`echo "$(date +%s) - $(cat /proc/uptime | cut -f 1 -d' ') + ${1}" | bc`
stamp=`echo "scale=0; ${unix_time} / 1" | bc`
echo ${stamp}
date -d @${stamp} "+%Y-%m-%d %H:%M:%S"
5. 脚本运行实例
# ./test.sh 18.204452 
1538100496.004452