Linux dmesg命令用於顯示開機信息。
kernel會將開機信息存儲在ring buffer中。您若是開機時來不及查看信息,可利用dmesg來查看。開機信息亦保存在/var/log目錄中,名稱為dmesg的文件里。
dmesg -T | grep -i oom
1、如果發現自己的java進程悄無聲息的消失了,幾乎沒有留下任何線索,那么dmesg一發,很有可能有你想要的。
[root@host-172-21-210-40 ~]# dmesg -T |grep score [Thu Oct 29 08:09:25 2020] Out of memory: Kill process 13062 (tidb-server) score 916 or sacrifice child [Thu Oct 29 08:10:51 2020] pd-server invoked oom-killer: gfp_mask=0x201da, order=0, oom_score_adj=0 [Thu Oct 29 08:10:51 2020] [ pid ] uid tgid total_vm rss nr_ptes swapents oom_score_adj name 可以看到tidb-server 被打分13062 被系統oom殺掉 解釋一下OOM killer(Out-Of-Memory killer),該機制會監控機器的內存資源消耗。當機器內存耗盡前,該機制會掃描所有的進程(按照一定規則計算,內存占用,時間等),挑選出得分最高的進程,然后殺死,從而保護機器。 dmesg日志時間轉換公式: log實際時間=格林威治1970-01-01+(當前時間秒數-系統啟動至今的秒數+dmesg打印的log時間)秒數: date -d “1970-01-01 UTC echo "$(date +%s)-$(cat /proc/uptime|cut -f 1 -d' ')+12288812.926194"|bc seconds”
2、列出加載到內核中的所有驅動
[root@host-172-21-210-40 ~]# dmesg | more [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 3.10.0-957.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Thu Nov 8 23:39:32 UTC 2018 [ 0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8 [ 0.000000] e820: BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000bffd9fff] usable [ 0.000000] BIOS-e820: [mem 0x00000000bffda000-0x00000000bfffffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved [ 0.000000] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved [ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000043fffffff] usable [ 0.000000] NX (Execute Disable) protection: active [ 0.000000] SMBIOS 2.8 present. [ 0.000000] DMI: Fedora Project OpenStack Nova, BIOS 1.9.1-5.el7_3.2 04/01/2014 [ 0.000000] Hypervisor detected: KVM [ 0.000000] e820: update [mem 0x00000000-0x00000fff] usable ==> reserved [ 0.000000] e820: remove [mem 0x000a0000-0x000fffff] usable [ 0.000000] e820: last_pfn = 0x440000 max_arch_pfn = 0x400000000 [ 0.000000] MTRR default type: write-back [ 0.000000] MTRR fixed ranges enabled: [ 0.000000] 00000-9FFFF write-back [ 0.000000] A0000-BFFFF uncachable [ 0.000000] C0000-FFFFF write-protect [ 0.000000] MTRR variable ranges enabled:
3、顯示所有被內核檢測到的硬盤設備
[root@host-172-21-210-40 ~]# dmesg | grep vdb [ 1.720171] virtio_blk virtio3: [vdb] 1048576000 512-byte logical blocks (536 GB/500 GiB) [ 1.721297] vdb: vdb1 [ 567.779935] EXT4-fs (vdb1): mounted filesystem with ordered data mode. Opts: nodelalloc
4. 清空dmesg緩沖區日志
我們可以使用如下命令來清空dmesg的日志。該命令會清空dmesg環形緩沖區中的日志。但是你依然可以查看存儲在‘/var/log/dmesg’文件中的日志。你連接任何的設備都會產生dmesg日志輸出。
[root@tecmint.com log]# dmesg -c
5. 實時監控dmesg日志輸出
在某些發行版中可以使用命令‘tail -f /var/log/dmesg’來實時監控dmesg的日志輸出。
[root@tecmint.com log]# watch "dmesg | tail -20"
