Skip to content

Instantly share code, notes, and snippets.

@582033
Created June 28, 2016 07:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 582033/b94dc1d4941f63bb28d837fa9c72f08d to your computer and use it in GitHub Desktop.
Save 582033/b94dc1d4941f63bb28d837fa9c72f08d to your computer and use it in GitHub Desktop.
树莓派获取[cpu温度/cpu使用率/内存空闲率]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
显示树莓派cpu温度、使用率及内存使用率
'''
import os
def show_temp():
file = open("/sys/class/thermal/thermal_zone0/temp")
temp = float(file.read()) / 1000
file.close()
print "Temp: %.1f ℃" %temp
def show_mem():
mem_cmd = "free -m | grep Mem | awk '{print ($4+$6)/$2}'"
mem_sur = round(float(os.popen(mem_cmd).read()), 2) * 100
print "Mem: %d%%" % mem_sur
def show_cpu():
cpu_cmd = "uptime | awk '{print $8,$9,$10,$11,$12}'"
cpu_used = os.popen(cpu_cmd).read()
print "Cpu: %s" % cpu_used.replace('\n', '')
if __name__ == '__main__':
show_temp()
show_cpu()
show_mem()
@zshanjun
Copy link

内存部分好像不准确

@zshanjun
Copy link

这是使用您的程序获得的结果:
Temp: 41.2 ℃
Cpu: average: 0.01, 0.02, 0.00
Mem: 90%

这是参考http://blog.csdn.net/huayucong/article/details/50389910获得的结果(其中有一些拼写的错误,已改正):
CPU Temperature = 41.2
CPU Use = 0.2

RAM Total = 882.0 MB
RAM Used = 99.0 MB
RAM Free = 783.0 MB

DISK Total Space = 15GB
DISK Used Space = 1.1GB
DISK Used Percentage = 8%

@lawlietsoul
Copy link

楼上CPU的结果也不对,原文只能获得上次boot时候的CPU,是一个定值

@lawlietsoul
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment