Error
While running the sudo apt update
and sudo apt upgrade
on a new Ubuntu Server 20.04.2 LTS 32-bit server OS for armhf architectures on the RaspberryPi with 4gb ram, I am encountering the following errors:
For sudo apt update
:
E: Release file for http://ports.ubuntu.com.../focal-updates/Inrelease is not yet valid (invalid for another 113d 22h 43min). Updates for this repository will not be applied.
For sudo apt upgrade
:
Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 3381 (unattended-upgr).
Code
The code below first checks if internet is available, if it is not, it checks if the wifi ssid and password have been added to the configuration, if not it prompts the user for them, and then activates those settings until the internet connection is established. Then it performs the sudo apt update
and sudo apt upgrade
commands.
# Verify internet access is available
while [ $(ping -q -w1 -c1 google.com &>/dev/null && echo online || echo offline) == offline ]
do
sleep 1
# get wifi module
module=$(ls /sys/class/net)
module_list=($module)
wlan_module=${module_list[-1]}
echo $wlan_module
# get wifi configuration file
config_filename=$(ls /etc/netplan)
echo $config_filename
config_filepath="/etc/netplan/$config_filename"
# check if the wifi is already added.
if ! grep -q "$wlan_module" "$config_filepath"; then
echo "have to add wifi"
# ask wifi pwd ssid
read -p "What is the wifi ssid you want to connect to(Case sensitive)?" ssid
# ask wifi pwd
read -p "What is the wifi pwd?" pwd
# append content
echo " wifis:" | sudo tee -a $config_filepath
echo " wlan0:" | sudo tee -a $config_filepath
echo " dhcp4: true" | sudo tee -a $config_filepath
echo " optional: true" | sudo tee -a $config_filepath
echo " access-points:" | sudo tee -a $config_filepath
echo " \"$ssid\":" | sudo tee -a $config_filepath
echo " password: \"$pwd\"" | sudo tee -a $config_filepath
# generate a config file
sudo netplan generate
# apply the config file
sudo netplan apply
fi
done
set timezone based on ip
export tz=wget -qO - http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/p'
&& timedatectl set-timezone $tz
export tz=timedatectl status| grep Timezone | awk '{print $2}'
Verify timezone is set correctly.
timedatectl status
yes | sudo apt update
read -p "Update completed <type enter>" next
yes | sudo apt upgrade
read -p "Upgrade completed <type enter>" next
the code is executed using:
sudo mkdir /media/usbstick
sudo mount /dev/sda1 /media/usbstick
/media/usbstick/.first_run.sh
Timezone details
In response to the comments I have included code that automatically sets the timezone, and I have manually verified the timezone is set correctly.
Assumption
I assume that that is because the Ubuntu server is automatically performing some kind of update/upgrading in the background in the background, which prevents me from running the sudo apt upgrade
command simultaneously.
Question
So when I manually kill the process that blocks the sudo apt upgrade
command with sudo kill -9 <process_id>
I am able to successfully run the sudo apt upgrade
command. However, I can imagine this is not the best way to ensure the upgrade command is completed successfully. So my current approach is to write a while-loop
that scans the output of the sudo apt upgrade
command until it yields a successful output. However, that feels like re-inventing the wheel and I can imagine there might exist better ways to resolve this issue. Hence, I would like to ask:
How can I run the sudo apt upgrade
command safely and automatically on a new installation of Ubuntu Server 20.04 LTS 32-bit server such that the rest of my Ubuntu Server post script can be completed correctly?
timedatectl status
will get you the current date/time on the system – Thomas Ward May 26 '21 at 12:16Local time:
,Universal time:
andTime zone:
are set correctly. The output errors remain the unchanged. So to explicitly answer the question by @FedonKadifeli, yes the date/time and timezone are correct. – a.t. May 26 '21 at 13:53