定时监测CPU温度并实现报警和自动关机

/proc/acpi/thermal_zone/THRM/temperature可以获取温度。

awk可以方便地按列处理文本。

 

具体的警报温度,关机温度,关机延迟和监测间隔时间可以自行设定,需要在root下才能实现关机,代码如下:

#!/usr/bin/python import os, commands, time warning_temperature = 70 shutdown_temperature = 80 interval = 30 shutdown_delay = 10 while True: statue, output = commands.getstatusoutput("cat /proc/acpi/thermal_zone/THRM/temperature | awk '{ print $2 }'") temperature = (int)(output) if temperature > shutdown_temperature: print "Current CPU temperature is " + str(temperature) + " and higher than shutdown_temperature." print "System will be halted in " + str(shutdown_delay) + " seconds." time.sleep(shutdown_delay) os.system("/sbin/shutdown -h now") elif temperature > warning_temperature: print "Current CPU temperature is " + str(temperature) + " and higher than warning temperature." time.sleep(interval)

 

你可能感兴趣的:(OS,System,output,delay)