3

I'm on Ubuntu Gnome 15.10.

I am regularly downloading large files (~800 MB), but I have a very slow Internet connection. It usually take a relatively long time to download it. Since I am not continuously working on the laptop, it automatically goes into sleep/hibernation mode.

When I press any key after it has gone to sleep/hibernation mode, it wakes up and takes me to the login screen.

I was able to find the power settings, which gave surprisingly few options:

power options

However, I am unsure about a couple of things

Is there a way in which my computer does not suspend during downloads, but my screen is still shut off when I am not working on it during download? Are there settings to be made to achieve this, or other solutions?

Jacob Vlijm
  • 83,767
AvZ
  • 145
  • 1
  • 7
  • This could solve your issue: http://askubuntu.com/questions/576525/is-there-any-way-to-make-ubuntu-not-to-suspend-while-a-download-in-progress/660295#660295 – Jacob Vlijm Dec 30 '15 at 16:55
  • @JacobVlijm So I just 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
  • Nono, just run it in the background, it's a background script, you can run it as a startup application. The advantage is that you can set all in one script, while it does not suspend selectively. See instructions in the answer. – Jacob Vlijm Dec 30 '15 at 17:07
  • @JacobVlijm I understand that but I would prefer the screen to be turned off after x minutes of inactivity while my downloads go on. Can this be done? – AvZ Dec 30 '15 at 17:12
  • would you fancy an edited version of the script, turning off the screen after x idle time? – Jacob Vlijm Dec 30 '15 at 18:25
  • @JacobVlijm That'd be fine. – AvZ Dec 30 '15 at 18:30
  • Hi @AvZ posted my answer, please let me know if all is clear (or not). – Jacob Vlijm Dec 30 '15 at 21:22

1 Answers1

0

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

  1. The script uses xprintidle:

    sudo apt-get install xprintidle
    
  2. Copy the script below into an empty file, save it as no_suspend.py

  3. 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
    
  4. 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
    #---
    
  5. Test- run the script with the command:

    python3 /path/to/no_suspend.py
    
  6. If all works fine, add it to your Startup Applications: Dash > Startup Applications > add the command:

    python3 /path/to/no_suspend.py
    
Jacob Vlijm
  • 83,767