当前位置: 首页>>技术教程>>正文


Bash 命令提示符中嵌入的 CPU 温度

问题描述

我想知道是否可以获取 CPU 温度并将其嵌入到命令提示符中。

这是我对 sensors 的输出:

$}-sensors
coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +55.0°C  (high = +87.0°C, crit = +105.0°C)
Core 0:         +55.0°C  (high = +87.0°C, crit = +105.0°C)
Core 1:         +52.0°C  (high = +87.0°C, crit = +105.0°C)

您能否向我展示如何使用 grep 函数将温度嵌入到我的命令提示符中?

最佳答案

是的,这是可能的,但具体细节取决于您的系统。在大多数情况下,命令 sensors 应该会显示它。

  1. 安装必要的软件包

    \n

    sudo apt-get install lm-sensors\n
  2. 运行 sensors-detect 并按照提示操作

    \n

    sudo sensors-detect\n
  3. 如果 sensors-detect 提示您安装任何额外的驱动程序。

  4. 运行 sensors 以确保其正常工作

    \n

    $ sensors\nacpitz-virtual-0\nAdapter: Virtual device\ntemp1:        +27.8\xb0C  (crit = +110.0\xb0C)\ntemp2:        +29.8\xb0C  (crit = +110.0\xb0C)\n\ncoretemp-isa-0000\nAdapter: ISA adapter\nPhysical id 0:  +63.0\xb0C  (high = +105.0\xb0C, crit = +105.0\xb0C)\nCore 0:         +62.0\xb0C  (high = +105.0\xb0C, crit = +105.0\xb0C)\nCore 1:         +63.0\xb0C  (high = +105.0\xb0C, crit = +105.0\xb0C)\n\nnct6776-isa-0a00\nAdapter: ISA adapter\nVcore:                  +1.86 V  (min =  +0.00 V, max =  +1.74 V)  ALARM\nin1:                    +1.36 V  (min =  +0.00 V, max =  +0.00 V)  ALARM\nAVCC:                   +3.33 V  (min =  +2.98 V, max =  +3.63 V)\n+3.3V:                  +3.33 V  (min =  +2.98 V, max =  +3.63 V)\nin4:                    +1.01 V  (min =  +0.00 V, max =  +0.00 V)  ALARM\nin5:                    +0.00 V  (min =  +0.00 V, max =  +0.00 V)\nin6:                    +0.21 V  (min =  +0.00 V, max =  +0.00 V)  ALARM\n3VSB:                   +3.31 V  (min =  +2.98 V, max =  +3.63 V)\nVbat:                   +3.18 V  (min =  +2.70 V, max =  +3.63 V)\nfan1:                     0 RPM  (min =    0 RPM)\nfan2:                  3292 RPM  (min =    0 RPM)\nSYSTIN:                  +0.0\xb0C  (high =  +0.0\xb0C, hyst =  +0.0\xb0C)  sensor = thermistor\nCPUTIN:                 +51.0\xb0C  (high = +80.0\xb0C, hyst = +75.0\xb0C)  sensor = CPU diode\nAUXTIN:                  +0.0\xb0C  (high = +80.0\xb0C, hyst = +75.0\xb0C)  sensor = CPU diode\nPCH_CHIP_CPU_MAX_TEMP:  +58.0\xb0C  (high = +80.0\xb0C, hyst = +75.0\xb0C)\nPECI Agent 0:           +60.0\xb0C  (high = +80.0\xb0C, hyst = +75.0\xb0C)\n                                 (crit = +105.0\xb0C)\nPCH_CHIP_TEMP:           +0.0\xb0C  \nPCH_CPU_TEMP:            +0.0\xb0C  \nintrusion0:            OK\nintrusion1:            OK\nbeep_enable:           disabled\n
  5. 解析输出以仅获取 CPU 温度。

    \n

    如上所示,我的系统上的输出与您的不同。但是,我们在这里关心的行是相同的。您可以使用以下命令获取 CPU 温度:

    \n

    $ sensors | grep -oP 'Physical.*?\\+\\K[0-9.]+'\n63.0\n
  6. 编辑您的 ~/.bashrc(或等效文件,如果您使用的是其他 shell)并添加运行上述命令的函数:

    \n

    show_temp(){\n    sensors | grep -oP 'Physical.*?\\+\\K[0-9.]+'\n}\n
  7. 在提示中使用该函数。例如:

    \n

    PS1="\\u@\\h $(show_temp) $ "\n

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14587.html,未经允许,请勿转载。