You are not logged in.

#1 2015-01-27 07:21:24

barbas
Member
Registered: 2015-01-16
Posts: 3

Check if a download is active

Hi there,

I'm quite sure it's been  asked and answered before, but really can't find anything.

My question is: I implemented suspending my archlinux-running laptop when idle for some time, but would like to avoid suspending if a download is ongoing. How can I have the system check such a thing?

Thanks a lot in advance for your help.

Offline

#2 2015-01-27 09:19:00

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Check if a download is active

The big question is what are you using for the "download". If it's a dedicated program like wget, aria2 etc., you can simply check for any such active processes before suspending.

Offline

#3 2015-01-27 09:19:42

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,532

Re: Check if a download is active

If you show us what are you doing then we may have a targeted answer.
For the best I would guess, you may look at either the PID of the download manager or the traffic on the computer ports, then tell the Upower to suspend the operations.
So what's your download manager and what is the power manager that you using ?


do it good first, it will be faster than do it twice the saint wink

Offline

#4 2015-01-27 18:56:59

mpan
Member
Registered: 2012-08-01
Posts: 1,376
Website

Re: Check if a download is active

A computer that does not download or upload anything. What a waste of hardware! wink

I don't know any particular software to do this, but you can detect low traffic on your network interface. See "/sys/class/net/your-interface/statistics/rx_bytes". If it doesn't change much through some time (like a minute), nearly nothing has been sent and you can assume download is complete. Of course iff nothing else is being downloaded and the download is not going at ridiculous low speed.

An example, prototipe script to achieve that:

#!/usr/bin/env bash

if (($# != 3)); then
    echo "$0 interface time-step-seconds treeshold-kib" >&2
    exit 1
fi

iface="$1"
timestep="$2"
threeshold="$3"
lastReading=$(cat "/sys/class/net/$iface/statistics/rx_bytes")

while true; do
    sleep "$timestep"
    currentReading=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
    speed=$(((currentReading - lastReading) / timestep / 1024))
    lastReading=$currentReading
    if ((speed < threeshold)); then
        # Do whatever you want
        exit 0
    else
        echo "Download speed: $speed KiB/s"
    fi
done

To run on interface xyz, waiting 60s between tests and considering values under 100KiB/s as "no download":

script xyz 60 100

Last edited by mpan (2015-01-27 18:57:32)


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

#5 2015-01-27 21:20:16

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,532

Re: Check if a download is active

One minute  test is too long. The system may go suspended, meanwhile.


do it good first, it will be faster than do it twice the saint wink

Offline

#6 2015-01-28 07:57:06

mpan
Member
Registered: 2012-08-01
Posts: 1,376
Website

Re: Check if a download is active

This is actually intended to go the other way around: disable suspend until the threshold is hit, at which point it is reenabled.

But even without that, once per minute is frequency high enough. How many people have suspend set to a minute? It would be ridicoulous. And 60s is just an example - one can perform the test even at 1Hz, but I believe this would lead to too many false positives.

Of course some scriptless solution would be better, but since no one provided the answer, this is currently the only working option proposed in the thread.

Last edited by mpan (2015-01-28 07:58:17)


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

Board footer

Powered by FluxBB