这是一个满足您需求的程序:
#!/usr/bin/env python
# coding: utf8
import time
import sys
import commands
USER = commands.getoutput("cat /var/log/auth.log | grep " +
sys.argv[0]).split("sudo:")[1].split()[0].strip()
def is_idle():
return int(commands.getoutput("xprintidle")) >= 10 * 60 * 1000
def is_night():
return time.localtime().tm_hour in (22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
def is_stumm():
return "RUNNING" not in commands.getoutput(("sudo -u %s pacmd " +
"list-sinks | grep RUNNING") % USER)
def main():
powersave = False
while 1:
if is_idle() and is_night() and is_stumm():
if not powersave:
print "going into powersave mode"
commands.getoutput("echo powersave > " +
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
powersave = True
else:
if powersave:
print "going into ondemand mode"
commands.getoutput("echo ondemand > " +
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
powersave = False
time.sleep(0.5)
if __name__ == '__main__':
assert commands.getstatusoutput("xprintidle")[0] == 0, (
"you need to `sudo apt-get install xprintidle`")
assert commands.getoutput("whoami") == "root", (
"you need to be root to run this program")
main()
注意您的偏好:
... >= 10 * 60 * 1000
... tm_hour in (22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
和
"RUNNING" not in ...
我正在使用Pulseaudio来查找电影是否正在播放。这也会影响音乐。