我想知道是否可以获取 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
函数将温度嵌入到我的命令提示符中?
答案1
是的,这是可能的,但具体细节取决于您的系统。在大多数情况下,命令sensors
应该会显示它。
安装必要的软件包
sudo apt-get install lm-sensors
运行
sensors-detect
并按照提示操作sudo sensors-detect
sensors-detect
如果需要,请安装任何额外的驱动程序。运行
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
解析输出以仅获取 CPU 温度。
如上所示,我的系统上的输出与您的不同。但是,我们在这里关心的行是相同的。您可以使用以下命令获取 CPU 温度:
$ sensors | grep -oP 'Physical.*?\+\K[0-9.]+' 63.0
编辑你的文件
~/.bashrc
(或者如果你使用另一个 shell,则编辑等效文件)并添加运行上述命令的函数:show_temp(){ sensors | grep -oP 'Physical.*?\+\K[0-9.]+' }
在提示中使用该函数。例如:
PS1="\u@\h $(show_temp) $ "
答案2
安装 lm 传感器:
sudo apt-get install lm-sensors
检测可用的传感器:
sudo sensors-detect
显示温度:
sensors