监控内核打印

1、dmesg

   这个命令主要查看开机信息,控制内核环形缓冲区。

用法:

   (1)dmesg 直接显示

   (2)  dmesg | tail 显示尾

   (3)dmesg -c 显示并清除部


2、用root权限执行 cat /proc/kmsg,这个命令不会马上结束,直到你手动ctrl+C为止,kmsg里面就是显示内核消息的,程序中printk的输出都可以看到,这样可以随时看到打印内容


3、用inotify监控log方式

代码如下所示:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/inotify.h>

#define NOTIFY_FILE "/var/log/kern.log"
#define EVENT_SIZE		(sizeof(struct inotify_event))
#define BUF_LEN			(10*(EVENT_SIZE + 16))
void main(void)
{
	int fd, wd,cnt;
	int readlen;
	char buf[BUF_LEN];
	struct inotify_event *event = NULL;

	fd = inotify_init();
	if(fd < 0)
	{
		return;
	}
	wd = inotify_add_watch(fd, NOTIFY_FILE, IN_MODIFY);
	
	printf("notify add watch wd %d",wd);
	
	while(1)
	{
		cnt = 0;
		readlen = read(fd, buf,BUF_LEN);

		if(readlen > 0)
		{
			system("tail -f /var/log/kern.log");
		}
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值