Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In some conditions like those :

  • Slow internet connections
  • Slow PPA or sources
  • Wifi internet access or 3g internet access

apt-get may stuck during update, install, upgrade or dist-upgrade.... endlessly (up to you to force it to close)

When I say stuck : it downloads the files, start the download, slow down and wait at some point and simply stop to download but still waiting the end of the file.

From what I understand it seems to occurs when there is a lot of latency variations (so when a server is saturated or with a wifi/3g internet access)

This effect affects also with official repositories. so it's not a source.list thing.

How can we tell to apt-get :

  1. to stop waiting endlessly
  2. retry to download when there is a timeout or a packet loss during the download

Thank you very much for the help.

Best regards,

P.D. : I am looking for a solution that does not involve brute force methods such as CTRL+C or kill. I am looking for something more compatible with scripts (so no "human" intervention when the apt-get line has been launched).

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can use the timeout command (installed by the package by the same name) to run a command and kill it if it takes more than N seconds. I'd be careful about when to use it though. Killing apt-get during a package installation could mess things up, so I suggest only running the download part with timeout. Something like this bash function:

upgrade() {
    local retry=5 count=0

    # retry at most $retry times, waiting 1 minute between each try
    while true; do

        # Tell apt-get to only download packages for upgrade, and send 
        # signal 15 (SIGTERM) if it takes more than 10 minutes
        if timeout -15 600 apt-get --assume-yes --download-only upgrade; then
        if (( count++ == retry )); then
            printf 'Upgrade failed\n' >&2
            return 1
        sleep 60

    # At this point there should be no more packages to download, so 
    # install them.
    apt-get --assume-yes upgrade

See How do I run a command, and have it abort (timeout) after N seconds? for more.

share|improve this answer
Interesting ! So I assume that apt-get really don't have that functionality integrated ? Thank you for your solution, It will be useful for more than apt-get ! :) –  Antoine Rodriguez Mar 20 '12 at 20:14

Have you tried just hitting Ctrl+C?

share|improve this answer
Yes and it works .... that's why I've said "up to you to force it to close" ;) I want something that avoids us to use CTRL+C or kill –  Antoine Rodriguez Mar 17 '12 at 15:35
At the downloading step, it's basically just wget in a for loop. –  maco Mar 17 '12 at 15:59
maybe .... however how can we tell him to consider this situation .... Since I use apt-get for years I always do a CTRL+C when it occurs. However when you want to script this there is many odds that you'll face this situation and will have a script that also runs endlessly .... It is weird that we can't tell to apt-get to abort the download if no data is received after X seconds or minutes .... –  Antoine Rodriguez Mar 17 '12 at 16:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.