Ubuntu – ny way to make Ubuntu not to suspend while a download in progress

downloadslock-screenpower-managementsuspend

Is there any way?

How to script in /usr/lib/pm-utils/sleep.d to check ${downspeed eth0}, ${upspeed wlan0}, ${upspeed eth0} and ${downspeed wlan0} and to set the system not to suspend while a download in progress but only turn screen off?

My OS is Ubuntu 15.04.

Best Answer

Inspired by @ByteCommanders second comment, the script below does what you describe: assuming the downloads folder is the directory you download into, it disables suspend during download, and subsequently waits for five minutes (arbitrary to set) before re- enabling suspend, to make sure the download is really finished.
You can set any other directory to be watched as download folder.

How it works

In a loop (once per 20 seconds), the script checks the size of the targeted folder with the command:

du -ks ~/Downloads

The script compares each check with the last one, to check for download activity (increase of size). If there is no activity for more then five minutes (but you can set any other "wait" time), the script assumes no download is going in, and "normal" suspend is (re-) enabled.

The other way around: if the script sees increasing size of the ~/Downloadsdirectory, it disables suspend, until no activity was detected for more then five minutes.

Low on juice

The script's commands are extremely low on resources. Since the cycle runs only once per 20 seconds (as it is), The processor load is practically none.

The script

#!/usr/bin/env python3
import subprocess
import time

#--- set suspend time below (seconds)
suspend_wait = 300
#---

#--- 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])

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")

check_1 = int(get("du -ks ~/Downloads").split()[0])

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. Copy the script below into an empty file, save it as no_suspend.py
  2. In the head section of the script, set the desired "normal" suspend time (since the script will re- enable suspend):

    #--- set suspend time below (seconds)
    suspend_wait = 300
    #---
    
  3. 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
    download_wait = 300  # time (seconds) to wait after the last download activity before suspend is re- activated
    #---
    
  4. Test- run the script with the command:

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

    python3 /path/to/no_suspend.py
    
Related Question