Created
June 28, 2016 07:02
-
-
Save 582033/b94dc1d4941f63bb28d837fa9c72f08d to your computer and use it in GitHub Desktop.
树莓派获取[cpu温度/cpu使用率/内存空闲率]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
楼上CPU的结果也不对,原文只能获得上次boot时候的CPU,是一个定值