On the edge of a dupe of this question. The difference with that question is however that you want the computer not suspended, but the screen still shut off during downloading large files nevertheless. Although seemingly a small difference, it does make a substantial difference in the answer (script).
About the solution
- The solution is a background script to run as a Startup Application, that will disable suspend for as long as the download takes.
At the same time, a second thread inside the script, keeps track of the idle time with the help of xprintidle
. xprintidle
is triggered by keyboard- and mouse events. After an arbitrary time, to set in the head of the script, the thread shuts down the screen(s), while the other (main) thread prevents suspend for as long as the download is active.
The download is noticed by a change in the size of the Downloads folder, as measured by periodically checks, using du -ks ~/Downloads
; if the size of the folder does not change any more during five minutes, the script assumes the download to be finished. It then and re- enables suspend.
Note
- As always (- should be) with background scripts, the additional processor load is nihil.
The script
#!/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
How to use
The script uses xprintidle
:
sudo apt-get install xprintidle
Copy the script below into an empty file, save it as no_suspend.py
In the head section of the script, set the desired "normal" suspend time (since the script will re- enable suspend), and the time before you'd like the screen to shut down:
#--- set suspend time below (seconds)
suspend_wait = 300 # = 5 minutes
#--- set time after which screen should blackout (seconds)
blackout = 300 # = 5 minutes
If you want, you can set other values:
#--- 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
#---
Test- run the script with the command:
python3 /path/to/no_suspend.py
If all works fine, add it to your Startup Applications: Dash > Startup Applications > add the command:
python3 /path/to/no_suspend.py
cron
the script every whatever minutes? However, instead of using a custom script, I'd prefer to use the features that come with Ubuntu for this purpose. (if there are any) – AvZ Dec 30 '15 at 17:07