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


18

我想知道是否有可能获得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功能将温度嵌入命令提示符吗?



qucik:cat / sys / class / thermal / thermal_zone0 / temp对我来说显示50000,当我的温度为50°C时
mondjunge

@mondjunge这些文件非常依赖于硬件。名称和路径都将根据您拥有的硬件而改变。在我的系统上,该文件显示了acpitz-virtual-0虚拟设备的温度,与我的CPU的温度有很大不同。
特登

您可以使用答案中的相同代码。它也将为您的sensors输出工作。
特登

@terdon:我知道,但没有时间解释,我只是在5分钟内标记重复的问题,并尝试提出快速解决方案。
mondjunge '16

Answers:


36

是的,可以,但是详细信息取决于您的系统。在大多数情况下,命令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文件(如果使用其他外壳,则为等效文件),并添加一个运行上述命令的函数:

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

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

在Ubuntu中安装lm-sensors真的有必要吗?默认安装在Debian中。
布里亚姆'16

1
@Braiam默认未在15.10或16.04中安装它。

1

安装lm传感器:

sudo apt-get install lm-sensors

检测可用的传感器:

sudo sensors-detect

显示温度:

sensors

5
据我了解的问题,OP希望CPU温度出现在其Bash提示符中,例如username@hostname (cputemp): /current/directory $-他们已经知道一般如何运行sensors命令,否则他们将如何显示问题中的输出?
字节指挥官

3
@ByteCommander是Bram的辩护,OP sensors在其原始问题中未提及,仅在我的答案的第一版中要求输出后才添加输出。当然,此答案并不能说明如何仅显示CPU温度,也不能说明如何将其包含在提示中,但是sensors在发布答案时,提及是有用的信息。
特登

在AMD系统上,我必须先在BIOS中打开“ IOMMU”,然后才能获得CPU温度(带有FX8300的MSI主板:超频设置-> CPU功能-> IOMMU,将“禁用”更改为“ 64 MB”(唯一其他选项))。
彼得·莫滕森
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.