温度和RAM监控脚本

我有以下脚本:

while : do clear; echo "---------------------------RAM Load------------------------------$ free -mt >> memory.txt; free -mt; echo "---------------------------Temperature---------------------------$ sensors >> temp.txt; sensors; sleep 1; clear; done 

获取当前系统RAM和温度并将其写入两个文件,分别为memory.txt和temp.txt

我想在带有Dygraphs的Web服务器上使用这些数据,这是清理数据的最佳方法,因为它提供了这两个文件:

memory.txt temp.txt

对于RAM部分,我只需要-/+ buffers/cache:行,而温度部分只需要温度。

您应该能够使用grep解决此问题。

要获得-/+ buffers/cache行,请将您的free命令更改为:

 free -mt | grep buffers/cache >> memory.txt; free -mt | grep buffers/cache; 

要获得温度,请尝试:

 sensors | grep °C >> temp.txt; sensors | grep °C; 

所以使用以下脚本:

 #!/bin/bash clear; echo "---------------------------RAM Load------------------------------$" free -mt | grep buffers/cache >> memory.txt; free -mt | grep buffers/cache; echo "---------------------------Temperature---------------------------$" sensors | grep °C >> temp.txt; sensors | grep °C; 

生成此输出:

 ---------------------------RAM Load------------------------------$ -/+ buffers/cache: 1545 449 ---------------------------Temperature---------------------------$ Physical id 0: N/A (high = +100.0°C, crit = +100.0°C) Core 0: N/A (high = +100.0°C, crit = +100.0°C) Physical id 2: N/A (high = +100.0°C, crit = +100.0°C) Core 0: N/A (high = +100.0°C, crit = +100.0°C) aploetz@dockingBay94:~$ 

要删除您不想要的额外文本,可以尝试使用awk

 $ free -mt | grep buffers/cache | awk '{print $3"\t"$4}' 1588 406 

温度会有点棘手,但可以用tr来完成。

 $ sensors | grep Physical | tr -d '(),' | awk '{print $7"\t"$10}' +100.0°C +100.0°C +100.0°C +100.0°C $sensors | grep Core | tr -d '(),' | awk '{print $6"\t"$9}' +100.0°C +100.0°C +100.0°C +100.0°C 

如果你想摆脱加号,只需在删除标志中加上’+’:

 $ sensors | grep Core | tr -d '(),+' | awk '{print $6"\t"$9}' 100.0°C 100.0°C 100.0°C 100.0°C 

除了Bryce的好建议之外,没有必要两次运行命令:

 free -mt | grep buffers/cache | tee -a memory.txt sensors | grep °C | tee -a temp.txt