0

I wrote a simple bash script to keep my system updated, and put it on a crontab to execute @reboot:

(sudo  apt-get update) >> /home/raphael/Documents/Logs/updateLogs.txt
(sudo  apt-get upgrade -y) >> /home/raphael/Documents/Logs/updateLogs.txt
(sudo apt autoremove -y) >> /home/raphael/Documents/Logs/updateLogs.txt
(sudo date ) >> /home/raphael/Documents/Logs/updateLogs.txt

However, every time I check the updateLogs.txt file I encounter these errors:

Err:1 http://packages.microsoft.com/repos/vscode stable InRelease
  Temporary failure resolving 'packages.microsoft.com'
Err:2 http://br.archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'br.archive.ubuntu.com'
Err:3 http://br.archive.ubuntu.com/ubuntu xenial-updates InRelease
  Temporary failure resolving 'br.archive.ubuntu.com'
Err:4 http://br.archive.ubuntu.com/ubuntu xenial-backports InRelease
  Temporary failure resolving 'br.archive.ubuntu.com'
Err:5 http://security.ubuntu.com/ubuntu xenial-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:6 http://packages.elementary.io/appcenter xenial InRelease
  Temporary failure resolving 'packages.elementary.io'
Err:7 http://ppa.launchpad.net/danielrichter2007/grub-customizer/ubuntu xenial InRelease
  Temporary failure resolving 'ppa.launchpad.net'
Err:8 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial InRelease
  Temporary failure resolving 'ppa.launchpad.net'
Err:9 http://ppa.launchpad.net/papirus/papirus/ubuntu xenial InRelease
  Temporary failure resolving 'ppa.launchpad.net'
Err:10 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial InRelease
  Temporary failure resolving 'ppa.launchpad.net'
Err:11 http://dl.google.com/linux/chrome/deb stable InRelease
  Temporary failure resolving 'dl.google.com'
Err:12 http://repository.spotify.com stable InRelease
  Temporary failure resolving 'repository.spotify.com'

If I run manually the script I get the expected results:

Hit:1 http://br.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://br.archive.ubuntu.com/ubuntu xenial-updates InRelease
Get:3 http://br.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Ign:4 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:5 http://repository.spotify.com stable InRelease
Hit:6 http://ppa.launchpad.net/danielrichter2007/grub-customizer/ubuntu xenial InRelease
Hit:7 http://dl.google.com/linux/chrome/deb stable Release
Hit:8 http://packages.microsoft.com/repos/vscode stable InRelease
Hit:10 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial InRelease
Hit:11 http://ppa.launchpad.net/papirus/papirus/ubuntu xenial InRelease
Hit:12 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial InRelease
Hit:13 http://packages.elementary.io/appcenter xenial InRelease
Get:14 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Fetched 204 kB in 5s (38,6 kB/s)

At first I thought it was a wi-fi problem, so I put an Ethernet cable in my notebook, but I got the same results. Any thoughts on why this happens?

1 Answer 1

1

Try to "wait" a bit before running apt update. You can empirically find optimal time to sleep, or you can use this code from this post:

#!/bin/bash

function check_online
{
    netcat -z -w 5 8.8.8.8 53 && echo 1 || echo 0
}

# Initial check to see if we're online
IS_ONLINE=check_online
# How many times we should check if we're online - prevents infinite looping
MAX_CHECKS=5
# Initial starting value for checks
CHECKS=0

# Loop while we're not online.
while [ $IS_ONLINE -eq 0 ];do
    # We're offline. Sleep for a bit, then check again

    sleep 10;
    IS_ONLINE=check_online

    CHECKS=$[ $CHECKS + 1 ]
    if [ $CHECKS -gt $MAX_CHECKS ]; then
        break
    fi
done

if [ $IS_ONLINE -eq 0 ]; then
    # We never were able to get online. Kill script.
    exit 1
fi

# Now we enter our normal code here. The above was just for online checking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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