You are not logged in.

#1 2009-10-24 07:54:03

fiod
Member
Registered: 2007-04-02
Posts: 205

[Solved]Do something when the download rate drops?

Hi,

I want to write a simple program or script that does the following:

Say I am downloading something. I want the program / script to recognize when the download rate drops below a certain
threshold (say, 10kb/s) and do something (say, shutdown the computer).

Can someone point me to the right direction?
How can I know the download rate?

Thanks
fiod

Last edited by fiod (2009-10-24 08:49:40)

Offline

#2 2009-10-24 08:11:22

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved]Do something when the download rate drops?

Total downloaded is in /sys/class/net/eth0/statistics/rx_bytes (uploaded is tx_bytes)

old=0;while sleep 5; do new=$(cat /sys/class/net/eth0/statistics/rx_bytes); diff=$(( (new-old)/5120 )); echo $diff kb/s; [ $diff -lt 10 ] && echo TERMINATE; old=$new; done

Offline

#3 2009-10-24 08:20:25

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: [Solved]Do something when the download rate drops?

Thanks a lot!

I will try figuring it out.

Whats the 5120 in "(new-old)/5120" by the way?

fiod

Offline

#4 2009-10-24 08:27:58

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved]Do something when the download rate drops?

5(seconds) * 1024(kb)

Offline

#5 2009-10-24 08:29:55

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: [Solved]Do something when the download rate drops?

It actually didn't work.

I did the following:

#!/bin/bash
old=0;
while sleep 5; do
new=$(cat /sys/class/net/eth0/statistics/rx_bytes);
diff=$(( (new-old)/5120 ));
echo $diff kb/s;
[ $diff -lt 1000 ] && echo TERMINATE;
old=$new;
done

(changed 10 to 1000 in "$diff -lt 10" to make sure it works).

And I got the following output:
It didn't immediately told me that the download rate is below 1000, but after a few seconds I got the following output:

[fio@delltux /tmp]$ ./tempscript
0 kb/s
TERMINATE
0 kb/s
TERMINATE
0 kb/s
TERMINATE

The download rate all this time was around 200 KB/s.

Tnx
fiod

Offline

#6 2009-10-24 08:34:51

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved]Do something when the download rate drops?

Do you use something other than eth0?

Offline

#7 2009-10-24 08:35:12

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: [Solved]Do something when the download rate drops?

Sorry, my bad..

Didn't switch "eth0" to "eth1".

Thanks a lot!

fiod

Offline

Board footer

Powered by FluxBB