Linux 日志出现kmsg,Linux下保存dmesg日志

1. 什么是dmesg

​ Linux内核是操作系统的核心,它控制对系统资源(例如:CPU、I/O设备、物理内存和文件系统)的访问。在引导过程中以及系统运行时,内核会将各种消息写入内核环形缓冲区,这些消息包括有关系统操作的各种信息。

​ 内核环形缓冲区是物理内存的一部分(意味着重新开机会重新生成,并不会累加保存),用于保存内核的日志消息,它具有固定的大小,这意味着一旦缓冲区已满,较旧的日志记录将被覆盖。考虑到开机信息非常重要,系统将开机信息也会另外保存到/var/log目录中dmesg文件中。

​ dmesg是一种程序,用于检测和控制内核环缓冲,程序用来帮助用户了解系统的启动和运行信息。kernel会将开机信息存储在ring buffer中,若是开机时来不及查看信息,可利用dmesg来查看

2. 怎么保存dmesg信息

dmesg的使用方法如下所示:

dmesg [OPTIONS]

详细信息可以通过输入 dmesg -h

在不带任何选项的情况下调用时,dmesg将所有消息从内核环形缓冲区写入标准输出

dmesg -T,打印人类可读的时间戳

3. QA

为了方便调查问题,怎么将dmesg信息都保存到系统的日志中去了?

​ 鉴于系统日志并不会持续性保存,应用程序为了方便调查问题,有时候又需要系统日志进行排除问题。这个时候就需要将系统日志保存到emmc等非丢失性设备中,比如可以通过下面的指令来保存系统日志。

[ -e $kotei_dmesg_cur_log ] && cat $kotei_dmesg_cur_log > $kotei_dmesg_last_log

cat /dev/kmsg > $kotei_dmesg_cur_log &

上述脚本的意思是:交替保存两份dmesg信息

dmesg和kmsg有什么区别?

dmesg is a program that dumps /proc/kmsg. In addition, it provides some filtering capabilities to weed out logs that the user isn't interested in.circular buffer is implemented by kernel itself, dmesg is just a util to read this circular via kernel interface /proc/kmsg

/proc/kmsg和/dev/kmsg有什么区别吗?

The kernel mechanisms have changed.

The kernel generates output to an in-memory buffer. Application softwares can access this in two ways. The logging subsystem usually accesses it as a pseudo-FIFO named /proc/kmsg. This source of log information cannot usefully be shared amongst log readers, because it is read-once. If multiple processes share it, they each get only a part of the kernel log data stream. It is also read-only.

The other way to access it is the newer /dev/kmsg character device. This is a read-write interface that is shareable amongst multiple client processes. If multiple processes share it, they all read the same complete data stream, unaffected by one another. If they open it for write access, they can also inject messages into the kernel's log stream, as if they were generated by the kernel.

/proc/kmsg and /dev/kmsg provide log data in a non-RFC-5424 form.

简单的说,程序中推荐优先使用/dev/kmsg

标签:kernel,log,kmsg,dmesg,保存,内核,Linux,日志

来源: https://www.cnblogs.com/yuzhenjin/p/14218188.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值