I am trying to write some custom messages in my dmesg output. I tried:
logger "Hello"
but this does not work. It exits without error, but no "Hello" appears int the output of:
dmesg
I am using a Fedora 9, and it seems that there is no syslogd/klogd daemon running. However, all my kernel messages are succesfully written in the dmesg buffer.
Any idea?
You can, as root, write to
/dev/kmsg
to print to the kernel message buffer:I've tested this on my server and an embedded Linux device, and it works on both, so I'm just going to assume it works pretty much everywhere.
dmesg
displays what is in the kernel buffer, whereaslogger
is forsyslogd
. I think if you want to print things into the kernel buffer you will need to create a driver that uses theprintk()
kernel function. If you just want it in/var/log/messages
, then with a "normal" setup I think what you have done withlogger
is already fine.The most basic example of a driver with
printk()
would be:hello.c:
Makefile:
Then:
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN121 for more...
Based on Kyle's module above:
To do a printk from user space:
@Calandoa's answer no longer works for Kernel +3.10. Combined his code, and the example code I found here. Then improved on the code quality...
Code saved to printk_user.c
Make using this Makefile
To do a printk from user space:
Based off of Kyle's answer, here is a quick tutorial showing how to do just that.
Figured I'd go ahead and include a full blown example of something that people can just compile and run for those that aren't as skilled with C based off of @BuvinJ 's answer
To run save the above as kmsg.c then execute
gcc kmsg.c -o kmsg
and run assudo ./kmsg "string you want to add to /dev/kmsg"
I just wanted some quick debugging messages in a daemon written by someone else in a cross complied kernel. I ran into a compile error trying to use
printk
, as<linux/module.h>
could not be included. Rather then battle with that excessively (to do this the right way) I cheated and used the following lazy, but functional 5 minute workaround:UPDATE (Thanks @glglgl!)
A much simpler version could be like this:
It now just takes strings, integrates them in a message to be written to that file and writes it.
Now that we talk about it, it can be even much easier: