Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to write a statement to dmesg. [How] can I do this?

share|improve this question
1  
Why do you want to do this? – Nils Apr 3 '12 at 20:36
5  
Does it matter? :) – stephelton Apr 3 '12 at 20:38
3  
Two reasons I've wanted to do this before: (1) to see what "now is" in dmesg-timestamp format, and (2) to know when I last looked at dmesg. – stephelton Apr 3 '12 at 20:39
    
I'm pretty sure now is seconds from boot, and I think it takes a kernel call to write to it so a userland program can't. – Kevin Apr 3 '12 at 21:22
    
@Kevin, so you know without looking how many seconds have elapsed since you booted? – stephelton Apr 4 '12 at 1:28
up vote 23 down vote accepted

Write to /dev/kmsg (not /proc/kmsg as suggested by @Nils). See linux/kernel/printk/printk.c devkmsg_writev for the kernel-side implementation and systemd/src/journal/journald-kmsg.c server_forward_kmsg for an example of usage.

share|improve this answer
    
It's not allowed on some system for non-root users (e.g.: Android kernel) :-(. – pevik Jan 7 at 11:20
    
E.g. date | sudo tee /dev/kmsg – sanmai Apr 11 at 3:46

Assuming nobody else comes up with an official way to do this ...

You can write a kernel module that calls the printk function. There's an example here that might just do the job for you.

share|improve this answer
6  
a. No need, others have written it already (e.g. kecho). b. Really no need for an extra module, see my answer. – ephemient Apr 3 '12 at 21:44

for BSDs

%logger -p kern.crit MESSAGE

(courtesy Ian, freebsd-questions mailing list)

for Linux

su root -c 'echo MESSAGE > /dev/kmsg'
share|improve this answer

If I understood man dmesg correctly, you should be able to write to /proc/kmsg.

share|improve this answer
3  
# echo "test" >> /proc/kmsg [yields =>] -su: echo: write error: Input/output error – stephelton Apr 3 '12 at 20:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.