Is there a way to pipe an output constructed of printk messges to grep on console?

161 Views Asked by At

I have an utility created that prints output using printk. For eg.

printmsg.c:
void main() {
     printk(KERN_DEBUG "Message alert id:XXXX!\n");
     printk(KERN_DEBUG "Message alert id:YYYY!\n");
     return;
}

If run this utility on the system, it prints the message like below on the console

#./printmsg
Message alert id:XXXX!
Message alert id:YYYY!

Whereas, if i run this application to pipe the output to grep, it doesn't work.

#./printmsg | grep XXXX
Message alert id:XXXX!
Message alert id:YYYY!

I am aware that the printk messages to kernel log buffer and dmesg to print the log buffer. With dmesg options to print output on stdoutput.

However, i am looking for an option to pipe the output to grep instantly when the utility runs and the output dumps on the console.

Limitation: printk can't be compromised here.

1

There are 1 best solutions below

1
Romeo Ninov On

You need to construct your shell script on this way:

#./printmsg 2>&1| grep XXXX

and this is because (with high probability) printk function send message to STDERR, not STDOUT