Linux:dmesg的时间戳转换为可阅读的形式

dmesg的时间戳

        dmesg 日志中的“时间”(18.204452) 表示的是系统启动到事件发生的时间差,这个值可以转换成时间戳:

unix_time=`echo "$(date +%s) - $(cat /proc/uptime | cut -f 1 -d' ') + 18.204452 " | bc`
echo ${unix_time}

注:/proc/uptime 第一列表示的是系统开机时间,根据这个值和当前时间可以获取到 dmesg 日志中事件发生的时间。

时间戳转换为对应的可读的时间

date -d "@${unix_time}" '+%Y-%m-%d %H:%M:%S'

转换脚本

#!/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`
echo ${unix_time}
date -d "@${unix_time}" '+%Y-%m-%d %H:%M:%S'

运行示例

# ./test.sh 18.204452
1538100496.004452
2018-09-28 10:08:16

扩展

dmesg -h 查看帮助
-T,–ctime 打印人类可读的时间戳

 #dmesg -T

猜你喜欢

转载自blog.csdn.net/hhd1988/article/details/128126105