39

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

  • 1
    Why do you want to do this? – Nils Apr 3 '12 at 20:36
  • 21
    Does it matter? :) – notlesh Apr 3 '12 at 20:38
  • 7
    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. – notlesh 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
  • 2
    @Kevin, so you know without looking how many seconds have elapsed since you booted? – notlesh Apr 4 '12 at 1:28
37

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.

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

for BSDs

%logger -p kern.crit MESSAGE

(courtesy Ian, freebsd-questions mailing list)

for Linux

su root -c 'echo MESSAGE > /dev/kmsg'
2

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.

  • 8
    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
-1

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

  • 4
    # echo "test" >> /proc/kmsg [yields =>] -su: echo: write error: Input/output error – notlesh Apr 3 '12 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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