Hello everyone. This is my second post. I don't usually write posts unless I have something good to share and this time I have something that surely many will be interested in.
A few months ago I was thinking of making a script to limit the bandwidth of applications, but I had some problems and errors that made it difficult for me, so I raised my doubts in the forum de <º FromLinux in case anyone had an idea.
So I left it for a while and one day I decided to resume the script. I did a lot of testing, I read a lot about bash, and I had headaches in my spare time, but I did it !!
I have enormous satisfaction and so I have decided to share my little script with you so that you can use and improve it. I plan to license it under GPLv3, but it's my first project so I'm not quite sure how to do this (I need advice from whoever has done it before).
Well, now I will explain what my need was and what I did to solve the problem.
I have an Internet plan of 512Kbs, therefore I use Transmission as a BitTorrent client to download large files (like LibreOffice and some GNU / Linux distros). With that speed the downloads take a long time and the problem is when using the web browser Firefox: takes a long time to load.
When I connect to the Internet, I activate the transmission upload and download time limits and wait for Firefox to load, then start the torrents again. As you will see, doing this a couple of times is tedious. Not to mention that sometimes I outright pause all torrents and then forget to turn them on again, leading to wasted valuable time for torrent upload / download.
For this problem I decided to create a Bash script that basically does the following:
1. Check that Transmission is running and no torrent is paused. If so, turn the torrents back on.
2. Verify that Firefox is running. Then it gets the KB / s sent and received by it and saves them in a file.
3. If the browser upload or download KB / s exceeds a reference range, the Transmission upload / download settings are changed.
This allows that when wanting to access a web page, when the browser sends the request, the transmission file upload is limited and when the page data is received, the download is limited. This works great when accessing multiple pages at once, and the results are actually the same as with Transmission disabled.
The great advantage is that it is fully automatic and does not require my intervention.
Although it could be used to control the bandwidth consumption of any application, at the moment it is only written to solve a very particular problem. But it is not very difficult to make modifications.
For it to work properly it is necessary to have the application «nethogs».
In this case, as the script works with Firefox and Transmission, it is necessary to have these applications installed, in addition to "transmission-remote" which is what changes the upload and download values of the torrents. I also use in the script "awk". I mention it because I don't know if all the distros have it installed.
A list of the applications that the script uses and the system where it works.
• Debian GNU / Linux 6.0.8
• Linux 2.6.32-5-686
• Firefox 24.0
• Transmission 2.03 (11030)
• Nethogs 0.7.0
It must be run as root because nethogs can only be run with that user, but transmission-remote it is executed with the normal user by means of the command his.
The script uses the internal Bash command trap With which it is possible to restore the transmission's load / unload default values, when it is stopped by means of the SIGINT (CTRL + c) or SIGTERM signals.
I'm still not sure how to do to run it at startup and stop when I shut down or restart the computer. I was thinking of putting a link in the file /etc/rc.local but I don't know if it would work, and I don't really understand how / etc / worksinit.d (I saw some scripts that are there, even skeleton, but I don't understand them). If anyone can help me, I will be very grateful.
Well folks, that's it. I hope that my little contribution is useful to you and that you can improve it if you wish. I will be happy to read your opinions and recommendations as well as suggestions in case there are security problems with the file (I am not a programmer, I only do a few things from time to time).
I know that with some fixes it can become a great application in the future, because I don't know of any program that does the same. For example it occurs to me that I could have a graphical interface in zenity and being able to choose the applications we want to prioritize in the use of bandwidth (web browser, update manager, file transfer, etc.) and the different reference values. Yes, it is somewhat ambitious, but quite useful.
You can download the script in the Easter. Thanks so much for reading!!