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");
}
}
}