I am looking for a script or a tool which I can turn on, when needed, which shuts down my computer if there is e.g. no network traffic for say 10 minutes, or below 100kb for example.
Would be really handy for automatic downloading. I know there are downsides to this, internet connection is hanging, download program is hanging, so if you have a better idea, please tell me.
Thanks in advance.
There are a few ways to go about this, I have written a very simple bash script that you can use to monitor the speed in KB p/s for a desired interface, when the download speed drops below the minimum (which you can set), then your computer will be shutdown.
Some things to keep in mind here are:
This is a bash script that I put together quickly, there are many different techniques to achieve the same result, however this is an easy one to understand and implement.
You will need to execute the bash script from cron as root, that means you need to open cron as the root user and add a cronjob as desired. The reason it needs to be in root's cron is that you will not be able to shutdown your computer from the command line without being root, and you cannot use sudo while you are away from keyboard. There are ways to get around it but I am trying to keep it as simple as possible.
I use a linux tool called ifstat, so you will need to install this otherwise the script will not work:
There are 2 options that you can modify in the script below, the INTERFACE and MIN_SPEED. The INTERFACE needs to be set to the interface you are using to download, either eth0 for your wired device or wlan0 for you wireless device, you can use the command ifconfig from the command line to see what interfaces you have available. The MIN_SPEED is set as desired, in my example I set it to the number 5, which means that if my download speed is less than 5 KB per second then my computer will shutdown.
Lastly, to improve on the script we could use a while loop and check the download speed over a specified period of time and if the average is less than the minimum we would shutdown, as well as run the script as a service, this is a more accurate way of approaching the problem and I will be happy to help you with that if this is the route you would like to follow.
Copy and paste the below code into a file in a directory of your choice on your computer, example i_speed.sh, then, very important, make the file executable, to do this from the command line, if your file was called i_speed.sh as follows:
Now you can sudo -i to root and setup your cronjob to call the script at time intervals that you desire.
Code to copy and paste into a file called i_speed.sh:
UPDATE
I wrote a small python program as an update to the bash script above which allows you to set additional variables such as retries and interval to get an average min speed over a specified period of time. Further updates will include a GUI for this program. Just copy and paste the code below into a file, example
download_monitor.py
then run it as followssudo python download_monitor.py
I found this topic very helpful. With no Python knowledge I've updated the above script to get average network speed and go into long sleep if average speed is more than minimum speed. After long sleep calculations are reset and average speed is calculated again.