3

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
  • "How can I detect an apt-get upgrade failure caused by no internet access?": I think this was intended to say "update" again, not "upgrade". Commented Apr 8, 2023 at 21:11

3 Answers 3

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.

2
  • 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! Commented Mar 5, 2016 at 13:42
  • you could also do something like apt-get update 2>&1 | tee /tmp/apt-get-errors for less typing
    – Jeff Schaller
    Commented Mar 5, 2016 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.)

1
  • 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 Commented Mar 5, 2016 at 7:21
1

I think the --error-on=any option does what is desired, though I have very little experience using it in practice so far. I think it's a relatively new feature of apt and apt-get:

-eany, --error-on=any
    Fail the update command if any error occured, even a transient one.
apt update -eany
apt update --error-on=any
apt-get update -eany
apt-get update --error-on=any

When I turn off my laptop's wifi and run apt update --error-on=any; echo $?, then the echo prints 100.

1
  • This is the proper solution, but unfortunately, this command line option is supported only since Debian Bullseye.
    – Michael F
    Commented Nov 10, 2023 at 9:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .