答案1
在被骗的边缘这个问题。这个问题的不同之处在于,你希望计算机不挂起,但在下载大文件时屏幕仍然关闭。虽然看似差别很小,但它做对答案(脚本)产生实质性的影响。
关于解决方案
- 解决方案是背景脚本作为启动应用程序运行,这样在下载过程中就会禁用暂停功能。
与此同时,第二线程在脚本内部,借助 来跟踪空闲时间
xprintidle
。xprintidle
由键盘和鼠标事件触发。在脚本头部设置的任意时间之后,线程会关闭屏幕,而另一个(主)线程会在下载处于活动状态时阻止暂停。通过使用 定期检查,可以通过下载文件夹大小的变化来判断下载是否
du -ks ~/Downloads
完成;如果文件夹大小在五分钟内没有再发生变化,则脚本认为下载已完成。然后重新启用暂停。
笔记
- 与往常一样(-应该如此),使用后台脚本时,额外的处理器负载为零。
剧本
#!/usr/bin/env python3
import subprocess
import time
from threading import Thread
def get_size():
return int(subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8").split()[0])
def get(cmd):
return subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
#--- set suspend time below (seconds)
suspend_wait = 300 # = 5 minutes
#--- set time after which screen should blackout (seconds)
blackout = 300 # = 5 minutes
#--- you can change values below, but I'd leave them as they are
speed_limit = 0 # set a minimum speed (kb/sec) to be considered a download activity
looptime = 20 # time interval between checks
download_wait = 300 # time (seconds) to wait after the last download activity before suspend is re- activated
#---
t = 0
key = ["gsettings", "get", "org.gnome.settings-daemon.plugins.power", "sleep-inactive-ac-timeout", "set"]
set_suspend = key[0]+" "+key[-1]+" "+(" ").join(key[2:4])
get_suspend = (" ").join(key[0:4])
check_1 = int(get("du -ks ~/Downloads").split()[0])
def get_idle(blackout):
shutdown = False
while True:
curr_idle = int(subprocess.check_output(["xprintidle"]).decode("utf-8").strip())/1000
time.sleep(10)
if curr_idle > blackout:
if shutdown == False:
subprocess.Popen(["xset", "-display", ":0.0", "dpms", "force", "off"])
shutdown = True
print("shutting down")
else:
pass
else:
shutdown = False
Thread(target=get_idle, args=(blackout,)).start()
while True:
time.sleep(looptime)
try:
check_2 = int(get("du -ks ~/Downloads").split()[0])
except subprocess.CalledProcessError:
pass
speed = int((check_2 - check_1)/looptime)
# check current suspend setting
suspend = get(get_suspend).strip()
if speed > speed_limit:
# check/set disable suspend if necessary
if suspend != "0":
subprocess.Popen(["/bin/bash", "-c", set_suspend+" 0"])
t = 0
else:
if all([t > download_wait/looptime, suspend != str(download_wait)]):
# check/enable suspend if necessary
subprocess.Popen(["/bin/bash", "-c", set_suspend+" "+str(suspend_wait)])
check_1 = check_2
t = t+1
如何使用
该脚本使用
xprintidle
:sudo apt-get install xprintidle
将以下脚本复制到一个空文件中,并将其另存为
no_suspend.py
在脚本的头部,设置所需的“正常”挂起时间(因为脚本将重新启用挂起功能)以及您希望屏幕关闭的时间:
#--- set suspend time below (seconds) suspend_wait = 300 # = 5 minutes #--- set time after which screen should blackout (seconds) blackout = 300 # = 5 minutes
如果你愿意,你能设置其他值:
#--- you can change values below, but I'd leave them as they are speed_limit = 0 # set a minimum speed (kb/sec) to be considered a download activity looptime = 20 # time interval between checks (in seconds) download_wait = 300 # time (seconds) to wait after the last download activity before suspend is re- activated #---
使用以下命令测试运行脚本:
python3 /path/to/no_suspend.py
如果一切正常,请将其添加到您的启动应用程序:Dash > 启动应用程序> 添加命令:
python3 /path/to/no_suspend.py