Script Bash: Control Transmission Bandwidth Automatically

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.

The script
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.

Software used
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!!

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

23 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   elav said

    Oh! Interesting 😀

    1.    eliotime3000 said

      In the lower left corner you have the option to limit the bandwidth that Transmission consumes both when uploading and downloading. With Transmission I have not had any problem with it.

      1.    cookie said

        But this is automatic, and there is also Transmission without a graphical interface so you may not have that function so handy.

      2.    Joaquin said

        Hello how are you.
        Yes I already know that. But that's exactly what I don't want to do.
        I have a very poor internet plan (512KB and it's the best in my area).

        With my script, I don't worry about doing everything by hand. Imagine that you open a page in the browser and Transmission is occupying all the bandwidth at that moment, so the page takes a while to load (at most 1 ′, but it is exasperating). I'm tired of "activating time limits" every two minutes. This is automatic and almost instantaneous (it checks the KB sent and received by Firefox every 5 seconds).

        Really in my case with that internet speed, it is excellent. Also I do not know of another application that does this, otherwise it would not have done it. The ones I've seen only limit bandwidth, but not automatically.

        I hope I was clear. Thank you for reading!

        1.    Joaquin said

          Sorry, I was wrong. They are not 512KB, rather they are Kbits. (That is, 1/2 "Mega"). The maximum I can download is 75KB / s and 50KB / s upload. That when the Internet is fine, otherwise the normal is 48 and 23.

    2.    Joaquin said

      Thank you!

  2.   cookie said

    Interesting, I use qBittorrent and use the switch for limits.

    The post looks nice with those colors 😉

    1.    Joaquin said

      Thanks for the colors. I'm not very good at it, it seems very long and monotonous with so much text.

  3.   George said

    Personally, I use transmission-daemon and firefox with a live application tab, so I don't go around with external programs or forgetting to continue the torrents or the activated turtle (and I can control it from my phone). To start a service, add a user (eg: adduser –disabled-password nethogs), create a text file for the daemon in /etc/init.d with a name (eg: nethogs-daemon) and then in debian type "update-rc.d nethogs-daemon defaults" so that it starts up by itself.

    The text file is a bash script too, which you can merge your script into.
    See if this can help you, it can be used to start transmission-daemon, it can be used to start nethogs

    1.    Joaquin said

      Let's see if I understand: this is to start Transmission at startup and stop it when shutting down or restarting. With that I have no problems since it always starts by itself and starts downloading (I added it to the applications at startup in Xfce).

      My problem is that if I place the script in rc.local or in init.d I don't know how the computer stops the script. That is, obviously when shutting down / restarting the PC, all processes are terminated (and with them Transmission and Nethogs as well) but I don't know what would happen to my script.

      And why does that bother me? The script creates a file in / tmp and controls the speed of Transmission. If I run it in a terminal and suddenly stop it (for example with CTRL + c), the script, before closing, restores the default speeds (if necessary) and then stops Nethogs and deletes the file from / tmp. I tried to make it as "professional" as possible so that it doesn't leave any loose files or background processes.

      About what you said, I didn't understand what "live application tab" is.

      1.    George said

        An application tab is a permanent tab, it is always open and minimized within firefox (contextual menu on tab, click on «paste tab»). When the daemons send TERM signal, they wait a while, until after they have closed session, and save their data. Note that in the link I gave before when exiting the script calls start-stop-daemon and tells transmission to stop, there you could paste "killall nethogs" and what is contained in StopScript. The init script in this case you would have to call it as root instead of a specific user, since it needs privileges.

        1.    Joaquin said

          Well thanks for the tip. As soon as I have time I will try it!

  4.   facundo said

    great, I have come across this inconvenience since even if you have a fast connection, you are forced to limit transmission while you want to browse and thus "waste" time.
    I'm going to try it when I can. Greetings and thanks!!

    1.    Joaquin said

      Thanks to you! Hope this can help you!

      1.    Facundo said

        Hello again Joaquin. I see that you are taking care of answering all the comments so I am going to take advantage of that and I am going to ask you a couple of newbie questions.
        First, how do I know if I have the "awk" script installed? I use Debian 7.2.
        Second, I don't know what to do with the paste code. I have to grab a text editor and save it to a file called "" and then run it as "./". This is the part where I am most lost.
        Third: When you say to run it as root, is it necessary to change from user to root or is it enough with sudo?

        If you have any reading to recommend me on the subject, I appreciate it.

  5.   Giskard said

    That's why I always uninstall Transmission and install Deluge. Well, for that and a little more things that Deluge offers me that the other does not bring.

    1.    Joaquin said

      Hi, I don't know Deluge. My main problem is the internet service. With this I squeeze it to the fullest.

  6.   panda said

    Hi. my bandwidth is also limited so i tried this script. But it's not working. the error seems to be in the nethogs -t command. returns the error "Waiting for first packet to arrive (see bug 1019381)" I tried mint, archlinux and nothing. What is that command supposed to return? I suppose I should print in plain text the bandwidth used by each application. Do you know another program that allows you to listen to the network?

    1.    Joaquin said

      Hello how are you.
      That is a Nethogs bug. It appears to me too, but it works anyway.

      What Nethogs does is show the processes that consume the most bandwidth, in real time. In the script, its output is saved in a text file "net.list", so that it can be filtered later.

      The script only works with Transmission (a BitTorrent client) and Firefox (web browser). What it does is limit Transmission's bandwidth when Firefox is loading a web page. You need to install: Nethogs, Transmission, Transmission-remote and Firefox.

      It's all explained in the post, so I don't know what your problem is.

      PS: Please write correctly and improve your spelling. It is a comment, not a text message.

      1.    panda said

        the script doesn't work for me. prints that error message many times. run "nethogs -t" in a terminal to see what it does but it doesn't print anything, just the error. on my pc it is not working. I imagine that it should print something similar to what it prints when run without the -t, showing the processes and bandwidth. but in my case it doesn't print any of that. which distro did you use to develop the script?

        1.    Joaquin said

          If you look closely, the post contains all the software used and its versions. The error I don't know why, but it appears to me every time "nethogs -t" is run. In the script it will happen every 2 ″.

          It may be that when you run nethogs, there is no process using the network and that is why you do not get any output.

          The script must be run as root because nethogs needs that user to function.

          now I remembered something important and I know why it doesn't work for you:

          YOU MUST EDIT the script and change the name of the normal user. In the script it is called "joaquin". You must change it to the name of your user.

          Sorry I did not realize that, I should have put the name in a variable. The thing is that I did not think of making it so general, I just wanted to show it to you so that you can see it and those who want can get ideas of how to do it. I did not do it with the intention that it would work on any computer, that takes time and I do not know how to program, some things happen to me.

          Good luck, anything ask again. And please read the post and comments of the script again.

  7.   Facundo said

    Hello Joaquín, tell me the following:

    transmission-remote: (http://localhost:9091/transmission/rpc/) Couldn't connect to server
    ioctl failed while establishing local IP for selected device eth0. You may specify the device on the command line.

    any ideas?? thanks!

    1.    Joaquin said

      Hello how are you.
      Excuse me but I have no idea 😀
      From what I understand the error says, it is a problem with ioctl, but I don't know what that is.

      Maybe you could try stopping the script and seeing if transmission-remote works with some of its options (read its man page with the "man" command).