How to save dmesg printing information to files in the background

Source: Internet
Author: User
Tags superuser permission dmesg

Dmesg debugging I information is often used as an important information for determining abnormal system exit. However, it is difficult to save the information when a system exits or restarts abnormally, so I had the idea of saving the dmesg debugging information to the file:

# Include <stdio. h> # include <unistd. h> # include <stdlib. h> # include <string. h> # include <time. h> // # include <Linux/Autoconf. h> // kernel-compiled configuration information # include <sys/klog. h> # include <sys/STAT. h> // # DEFINE _ log_buf_len (1 <config_log_buf_shift) // in kernel 2.6.28, the default value is 1 <17. This is the real dmesg buffer size. # DEFINE _ log_buf_len (1 <17) // in kernel 2.6.28, the default value is 1 <17. This is the size of the real dmesg buffer. # DEFINE _ log_path "/home/default/dmesg. log "# define log_sleep (x) (sleep (x) # DEFINE _ log_size 10485760 // delete the file when it is greater than 10 m # define buf_size 256 long check_log_size (void) {struct stat f_stat; If (STAT (_ log_path, & f_stat) =-1) {return-1;} return (long) f_stat.st_size;} int main (INT argc, char * argv []) {char Buf [_ log_buf_len] = {0,}; char tmpbuf [buf_size] = {0,}; int ret = 0; file * fp = NULL; struct TM * PTR; time_t lt ;/ /Daemon (0, 0); // enter the daemon mode while (1) {log_sleep (120); // sleep 10 s fp = fopen (_ log_path, "A + "); // append open if (null = FP) {printf ("creat file faild! \ N "); continue;} ret = klogctl (4, Buf ,__ log_buf_len); // obtain dmesg information. This function requires the superuser permission to run if (0> = RET) {perror ("klogctl"); fclose (FP); continue;} lT = Time (null); // obtain the time PTR = (struct TM *) localtime (<); sprintf (tmpbuf, "[log time:] % s", asctime (PTR); // record the time printf ("tmpbuf = % s \ n", tmpbuf ); fwrite (tmpbuf, strlen (tmpbuf), 1, FP); fwrite (BUF, strlen (BUF), 1, FP); fflush (FP); fclose (FP ); if (_ log_size <check_log_size () {unlink (_ log_path); // Delete this file} memset (tmpbuf, 0, buf_size); memset (BUF, 0, __log_buf_len);} return 0 ;}


CC := arm-none-linux-gnueabi-gccall:dmesg_testdmesg_test:dmesg_test.c$(CC) dmesg_test.c -o dmesg_test -Wallinstall:cp dmesg_test /nfsroot/updateclean:rm dmesg_test

Run: dmesg_test &

The program is executed in the background, and the dmesg information is read every 120 seconds.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.