脚本代码
#文件名:status.py 路径:~/
import os
#Get CPU temperature
def getCPUTemperature():
with open("/sys/class/thermal/thermal_zone0/temp") as tempFile:
return str(float(tempFile.read())/1000)
#Get RAM status
def getRAMInfo():
p = os.popen('free -m')
i = 0
while 1:
i = i + 1
line = p.readline()
if i == 2:
return(line.split()[1:4])
#温度
_temperature = getCPUTemperature()
#内存
RAM_stats = getRAMInfo()
RAM_total = RAM_stats[0]
RAM_used = RAM_stats[1]
RAM_free = RAM_stats[2]
#字典
show_status_list = {'Temperature(C):' : _temperature, 'RAM Total(MB):':RAM_total, 'RAM Used(MB):':RAM_used, 'RAM Free(MB):':RAM_free}
if __name__ == '__main__':
for k,v in show_status_list.items():
print("{:18s}".format(k) + v)
运行
python3 stayus.py
加入默认命令
sudo vim ~/.bashrc
在最后加上
alias status='python3 ~/status.py'
刷新下环境变量
source ~/.bashrc
效果
$ status
Temperature(C): 39.433
RAM Total(MB): 3776
RAM Used(MB): 127
RAM Free(MB): 3233