1

I have a script which runs the command apt-get update but the script is unable to detect failure due to no internet.
i.e. if apt-get is not run with root privileges it exits with code 100.
so I can do a check like

if apt-get update
then
    echo "success!"
else
    echo "something went wrong
fi

But if apt-get update is run without an internet connect, it produces error messages, some to STDOUT, some to STDERR, e.g.

this goes to STDERR

...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/InRelease
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/trusty-security/InRelease
...

This goes to STDOUT

...
Err http://archive.ubuntu.com trusty-updates Release.gpg
  Temporary failure resolving 'archive.ubuntu.com'
Ign https://deb.nodesource.com trusty InRelease
Ign https://deb.nodesource.com trusty Release.gpg
...

But ultimately it still returns an exit of 0.
How can I detect an apt-get upgrade failure caused by no internet access?

1

Here's a stub that may work a little bit better:

if apt-get update 2> /tmp/apt-get-errors
then
    if grep -q "^W: Failed to fetch" /tmp/apt-get-errors
    then
      echo "success!"
    else
      echo apt-get failed to fetch
    fi
else
    echo "something went wrong
fi
rm /tmp/apt-get-errors

The basic idea is to catch apt-get's stderr output into a file, then search that file for known-bad patterns before declaring success.

It could be improved with the use of mktemp and possibly a better grep pattern, but I don't have a way to test apt-get right now.

  • 1
    thanks, I had the same idea, I think it should work for detecting the broken internet connection. Although non-critical warnings also go to STDERR. eg if you added a repo twice. apt (assuming its connected to the internet) can still function but normally but I would want the error printed in the terminal so you might need some complicated expression like if apt-get update 2>&1 1>/dev/tty | tee > /tmp/apt-get-errors .Im thinking per the previous answer, of just performing a test for internet connectivity as a separate check beforehand - and if no internet - dont even bother with apt-get! – the_velour_fog Mar 5 '16 at 13:42
  • you could also do something like apt-get update 2>&1 | tee /tmp/apt-get-errors for less typing – Jeff Schaller Mar 5 '16 at 14:03
1

I'm not sure this is fool-proof but I suppose you could test if the Internet connection is working, but I am not sure if an error is caused by no Internet connection.

cd [directory]
wget [just download any file aslong as you know the name.]
[ -f /[currentDir]/[filename] ] && wall "Internet Connection Is Existant" || wall "Any errors are likely caused by internet issues."
rm [file name just so it doesn't mess things up later.]

And you could change the "wall" to echo's I just like annoying my friends since I use SSH a lot.. (1/2 debian and 1/2 time on SSH.. On a terminal application or shell.)

  • thats a pretty good answer for a self confessed newb. Its close but I wouldnt use this becuase wget or similar tools can introduce their own problems. e.g. I have a curl command in a script that every 10th or so run ut just fails because there are alot of things unrelated to internet connectivity that can cause wget to fail. e.g. DNS . so on some curl commands that are important Ill have the script just sit there retrying over and over in case of "random" failures – the_velour_fog Mar 5 '16 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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