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

Modified on: Sat, 09 Jun 2018 08:26:23 +0800

我想知道是否有可能获得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. 安装必要的软件包

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

    sudo sensors-detect
    
  3. 如果sensors-detect告诉您,请安装任何额外的驱动程序。

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

    $ sensors
    acpitz-virtual-0
    Adapter: Virtual device
    temp1:        +27.8°C  (crit = +110.0°C)
    temp2:        +29.8°C  (crit = +110.0°C)
    
    coretemp-isa-0000
    Adapter: ISA adapter
    Physical id 0:  +63.0°C  (high = +105.0°C, crit = +105.0°C)
    Core 0:         +62.0°C  (high = +105.0°C, crit = +105.0°C)
    Core 1:         +63.0°C  (high = +105.0°C, crit = +105.0°C)
    
    nct6776-isa-0a00
    Adapter: ISA adapter
    Vcore:                  +1.86 V  (min =  +0.00 V, max =  +1.74 V)  ALARM
    in1:                    +1.36 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    AVCC:                   +3.33 V  (min =  +2.98 V, max =  +3.63 V)
    +3.3V:                  +3.33 V  (min =  +2.98 V, max =  +3.63 V)
    in4:                    +1.01 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    in5:                    +0.00 V  (min =  +0.00 V, max =  +0.00 V)
    in6:                    +0.21 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    3VSB:                   +3.31 V  (min =  +2.98 V, max =  +3.63 V)
    Vbat:                   +3.18 V  (min =  +2.70 V, max =  +3.63 V)
    fan1:                     0 RPM  (min =    0 RPM)
    fan2:                  3292 RPM  (min =    0 RPM)
    SYSTIN:                  +0.0°C  (high =  +0.0°C, hyst =  +0.0°C)  sensor = thermistor
    CPUTIN:                 +51.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
    AUXTIN:                  +0.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
    PCH_CHIP_CPU_MAX_TEMP:  +58.0°C  (high = +80.0°C, hyst = +75.0°C)
    PECI Agent 0:           +60.0°C  (high = +80.0°C, hyst = +75.0°C)
                                     (crit = +105.0°C)
    PCH_CHIP_TEMP:           +0.0°C  
    PCH_CPU_TEMP:            +0.0°C  
    intrusion0:            OK
    intrusion1:            OK
    beep_enable:           disabled
    
  5. 解析输出以仅获得CPU温度。

    如上所示,我系统上的输出与您的输出不同。但是,我们关心的这条线是一样的。您可以通过以下方式获得CPU温度:

    $ sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
    63.0
    
  6. 编辑~/.bashrc(或等效文件,如果你正在使用另一个shell)并添加一个运行上述命令的函数:


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

    show_temp(){
        sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
    }
    
  8. 醇>
作者:,terdon

相关问答

添加新评论