liranetn
Posts: 2
Joined: Fri May 14, 2021 1:12 pm

AutoUpdate Pi OS

Fri May 14, 2021 1:16 pm

Hi All,

I am looking for a way to automatically update pi OS every week.
Currently I just log to the OS and run terminal:
sudo apt update
sudo apt full-upgrade

any ideas?

terribleted
Posts: 254
Joined: Tue Oct 06, 2020 8:07 pm

Re: AutoUpdate Pi OS

Sat May 15, 2021 1:39 pm

create a script with the update commands.
suggest you redirect the output to a log file, so you can review....
here is the script i use:

Code: Select all

#!/bin/sh
#
#  Simple updater for PI 4 NO OVERLAY
#  WIRED ethernet must be UP
# terribleted  Feb 2011
# note: creates a log file named todays date.txt ("$mynow")


mynow=$(date '+%Y%m%d').txt
myup=$(cat /sys/class/net/eth0/operstate)
lookfor="up"
date > "$mynow"
if [ "$myup" = "$lookfor" ]; then
	
	echo "---- ETH0 wired is UP" | tee -a "$mynow"
	echo "---- UPDATE" | tee -a "$mynow"
	sudo apt-get update | tee -a "$mynow" 
	echo " " | tee -a "$mynow"
	echo "---- UPGRADE" | tee -a "$mynow"
	sudo apt-get -y full-upgrade 2>&1 | tee -a "$mynow" 
			if [ $? != 0 ]; then
				
				echo output of upgrade errorcode is $? | tee -a "$mynow"
				echo Error in upgade... running fix-broken | tee -a "$mynow"			
				# errors, try to fix broken packages
				sudo apt-get -y --fix-broken install 2>&1 | tee -a "$mynow"
				echo .....done running fix-broken | tee -a "$mynow"
			fi

	echo " " | tee -a "$mynow"
	echo "---- AUTOREMOVE" | tee -a "$mynow"
	sudo apt-get -y autoremove 2>&1 | tee -a "$mynow"
	echo " " | tee -a "$mynow"
	echo "---- AUTOCLEAN" | tee -a "$mynow"
	sudo apt-get autoclean 2>&1 | tee -a "$mynow"
		# get firmware status
		# note that this saves to a file, which is later deleted
		sudo rpi-eeprom-update -m $PWD/firmstat.txt
		foo=$(cat $PWD/firmstat.txt)
		echo Firmware Versions: | tee -a "$mynow"
		echo $foo | tee -a "$mynow"
		myfirm1=$(echo $foo | cut -d'"' -f 4)
		myfirm2=$(echo $foo | cut -d'"' -f 6)
		
		if [ "$myfirm1" = "$myfirm2" ]; then
			# firmware ver match
			echo " Firmware UP TO DATE- Exiting" | tee -a "$mynow"
			rm -f $PWD/firmstat.txt
			#sleep 5
		else
			# firmware vers do not match - UPdate
			echo " Firmware NOT UPDATED" | tee -a "$mynow"
			#Get Yes/no answer
			# NOTE: Disabled below
			CONT="n"
			#read -p "Update the FIRMWARE (y/n)?" CONT
			echo answer to 'update firmware' was: $CONT | tee -a "$mynow" 
			if [ "$CONT" = "y" ]; then
				echo " UPDATING FIRMWARE" | tee -a "$mynow" 
				sudo rpi-eeprom-update -a 2>&1 | tee -a "$mynow"
			else
				echo "Firmware NOT updated" | tee -a "$mynow"
				
			fi
			rm -f $PWD/firmstat.txt
		fi
	sleep 5
	echo "---- DONE! " >> "$mynow"
else
	echo "---- ETH0 wired is DOWN - NO UPDATES PROCESSED" | tee -a "$mynow"
fi
THEN:
set up a cron job to run the script weekly.

NOTE: i just noticed the firmware update section asks for input. that won't work in a cron job....
i run the script manually.
you could delete the line asking for READ. and set CONT="y"
then it should work.... i think

Note2: i disabled the RPI-eeprom update by hard coding a "n" to the $CONT variable.
this should work now in a cron job..... the result will be logged, so you will know if the eeprom needs updating (when you review the log file)
i had a Trash-80 model 1 (circa 1980). upgrading from 4k to 16k of RAM, i thought "i'll never use this much RAM".
Now i have a computer with a million times that much memory. And i keep running out of it.
Not the computer....ME.

terribleted
Posts: 254
Joined: Tue Oct 06, 2020 8:07 pm

Re: AutoUpdate Pi OS

Sun May 16, 2021 3:24 pm

UPDATE:
ok, tested. Added some code to wait for ETH0 to come up. (if not up, wait 30 sec.... i needed that due to my testing @reboot)
INSTALLATION:
- Create a folder "updater" under your PI account.
copy this script into it... name it "rpi-updater.sh"

Then add a cron job to launch it.... with crontab -e
remember to put the full path (example 0 2 * * 0 /home/pi/updater/rpi-updater.sh)
above example will run at 0200hrs, on Sunday

Code: Select all

#!/bin/sh
#
#  Simple updater for PI 4 NO OVERLAY
#
#

mynow=$(date '+%Y%m%d').txt
mynow="/home/pi/updater/"$mynow

myup=$(cat /sys/class/net/eth0/operstate)
lookfor="up"
date > "$mynow"

if [ "$myup" = "$lookfor" ]; then
	# do nothing
	echo "---- ETH0 wired is UP" | tee -a "$mynow"
else
	# ETH0 not up, wait 30 sec
	echo "---- ETH0 wired is DOWN - Waiting 30 Sec" | tee -a "$mynow"
	sleep 30
fi

#re-check state of ETH0
myup=$(cat /sys/class/net/eth0/operstate)

if [ "$myup" = "$lookfor" ]; then
	
	echo "---- ETH0 wired is UP" | tee -a "$mynow"
	echo "---- UPDATE" | tee -a "$mynow"
	sudo apt-get update | tee -a "$mynow" 
	echo " " | tee -a "$mynow"
	echo "---- UPGRADE" | tee -a "$mynow"
	sudo apt-get -y full-upgrade 2>&1 | tee -a "$mynow" 
			if [ $? != 0 ]; then
				
				echo output of upgrade errorcode is $? | tee -a "$mynow"
				echo Error in upgade... running fix-broken | tee -a "$mynow"			
				# errors, try to fix broken packages
				sudo apt-get -y --fix-broken install 2>&1 | tee -a "$mynow"
				echo .....done running fix-broken | tee -a "$mynow"
			fi

	echo " " | tee -a "$mynow"
	echo "---- AUTOREMOVE" | tee -a "$mynow"
	sudo apt-get -y autoremove 2>&1 | tee -a "$mynow"
	echo " " | tee -a "$mynow"
	echo "---- AUTOCLEAN" | tee -a "$mynow"
	sudo apt-get autoclean 2>&1 | tee -a "$mynow"
		# get firmware status
		# note that this saves to a file, which is later deleted
		sudo rpi-eeprom-update -m $PWD/firmstat.txt
		foo=$(cat $PWD/firmstat.txt)
		#echo $foo | tee -a "$mynow"
		myfirm1=$(echo $foo | cut -d'"' -f 4)
		myfirm2=$(echo $foo | cut -d'"' -f 6)
		echo Firmware Versions: | tee -a "$mynow"
		echo Current Firmware: $myfirm1 | tee -a "$mynow"
		echo latest Firmware: $myfirm2 | tee -a "$mynow"
		
		if [ "$myfirm1" = "$myfirm2" ]; then
			# firmware ver match
			echo " Firmware UP TO DATE- Exiting" | tee -a "$mynow"
			rm -f $PWD/firmstat.txt
			#sleep 5
		else
			# firmware vers do not match - UPdate
			echo " Firmware NOT UPDATED" | tee -a "$mynow"
			#Get Yes/no answer
			#read -p "Update the FIRMWARE (y/n)?" CONT
			CONT="y"
			echo answer to 'update firmware' was: $CONT | tee -a "$mynow" 
			if [ "$CONT" = "y" ]; then
				echo " UPDATING FIRMWARE" | tee -a "$mynow" 
				sudo rpi-eeprom-update -a 2>&1 | tee -a "$mynow"
			else
				echo "Firmware NOT updated" | tee -a "$mynow"
				
			fi
			rm -f $PWD/firmstat.txt
		fi
	sleep 5
	echo "---- DONE! " >> "$mynow"
else
	echo "---- ETH0 wired is DOWN - NO UPDATES PROCESSED" | tee -a "$mynow"
fi
i had a Trash-80 model 1 (circa 1980). upgrading from 4k to 16k of RAM, i thought "i'll never use this much RAM".
Now i have a computer with a million times that much memory. And i keep running out of it.
Not the computer....ME.

User avatar
craigevil
Posts: 208
Joined: Wed Jan 27, 2021 5:22 am
Location: OZ

Re: AutoUpdate Pi OS

Sun May 16, 2021 3:28 pm

sudo apt install unattended-upgrades
Description: automatic installation of security upgrades
This package can download and install security upgrades automatically
and unattended, taking care to only install packages from the
configured APT source, and checking for dpkg prompts about
configuration file changes.
or sudo apt install cron-apt
Description: automatic update of packages using apt-get
Contains a tool that is run by a cron job at regular intervals. By default it
just updates the package list and downloads new packages without installing.
You can instruct it to run anything that you can do with apt-get
(or aptitude).
No need to manually create a script the tools are already there.
Raspberry PI 400 Raspberry Pi OS (Unstable) Kernel: 5.10.17-v8+ aarch64 DE: MATE Ram 4GB
Debian - "If you can't apt install something, it isn't useful or doesn't exist"

liranetn
Posts: 2
Joined: Fri May 14, 2021 1:12 pm

Re: AutoUpdate Pi OS

Mon May 17, 2021 8:29 am

Thanks!!!!

User avatar
Botspot
Posts: 1728
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas
Contact: Website

Re: AutoUpdate Pi OS

Tue May 18, 2021 3:29 pm

If you use your Pi as a daily desktop machine, then consider using Update Buddy. This regularly checks for apt updates on boot. If there are any, Update Buddy displays a non-intrusive notification.
If you click "Details", a list of updatable packages will be displayed.
Image
Click "Update Now" to perform an apt upgrade in the terminal.

I made this tool for my own benefit. (Inspired by pi-top's simpler, more annoying autostarted updater)
Install instructions can be found here: https://github.com/Botspot/update-buddy
Pi-Apps - #1 most popular Raspberry Pi app store with >100,000 users.
Includes the Zoom desktop app, 3 ways to run Minecraft, 7 web browsers, appearance themes, and 72 more.
More info: https://github.com/Botspot/pi-apps

Return to “Troubleshooting”