title: What happens in BASH when you do Ctrl-C (hint, it's not simply sending a SIGINT)
body: <p>A little background first - When I do <code>apt-get install</code> downloads from my company internet it provides a high burst of speed (400-500KB/s) for the first 10 seconds or so before dropping down to a tenth of that (40-50KB/s), and then after a few minutes to a truly miserable (4-5KB/s). This makes me think that the sysadmin has implemented some sort of a network throttling scheme.</p>
  
  <p>Now I know that the network is not simply erratic, because if I start an <code>apt-get install foo</code>, <code>Ctrl-C</code> it after 10 seconds and immediately run <code>apt-get install foo</code> again (by doing an up arrow and enter to use bash history), and then <em>keep repeating this process for a few minutes till all packages are downloaded</em>, I can download even large packages very fast. In particular, even after aborting a download with Ctrl-C, apt-get seems to be able to resume the download in the next invocation.</p>
  
  <p>Of course, staring at the screen doing Ctrl-C Up Enter every 10 seconds gets really boring real fast, so I wrote a shell script -</p>
  
  <pre><code>#!/bin/sh
  for i in `seq 1 100` ; do
      sudo apt-get install foo -y &amp;
      sleep 10
      sudo kill -2 $!
  done
  </code></pre>
  
  <p>This seems to work. It spawns apt-get, runs it for 10 seconds and then kills (by sending a SIGINT) it and starts it up again. However, it doesn't really work because now apt-get does not resume downloads on subsequent invocations!</p>
  
  <p>An an experiment I ran <code>sudo apt-get install foo</code> from one terminal and then ran <code>kill -2 &lt;PID of apt-get&gt;</code> from another terminal. And even in that case, when I restart apt-get, it does not resume the download.</p>
  
  <p>So clearly a Ctrl-C is <strong>not</strong> equivalent to SIGINT. And something else is happening when I do Ctrl-C manually which gives apt-get a chance to save the state of the download. The question is - what is it?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>These are the suggestions I have received so far, but no cigars. The mystery deepens! -</p>
  
  <ol>
  <li><p>On <code>sudo kill -2 $!</code> the signal might be going to <code>sudo</code> instead of <code>apt-get</code>. This is not the reason because as mentioned above I also tried sending SIGINT specifically to apt-get's PID and even that prevented apt-get from saving its state.</p></li>
  <li><p>Sudo catches the signal and sends some other signal to apt-get. I tried sending apt-get all the signals I can think of! It still does not resume the download for any of them. It only resumes downloads when I do Ctrl-C to kill it.</p></li>
  <li><p>Apt-get handles SIGINT differently if it is from a script instead of an interactive shell. Again, the "experiment" above proves that this is not true.</p></li>
  </ol>