User:Thorko/Automatically update system

From ArchWiki

This article is not officially supported.

The Arch Linux community does not offer support for the information contained in this page; for installation procedures, the Installation guide is the only officially supported document. The content below is mainly maintained by User:Thorko, who last reviewed it on 2024-06-19. This approach is unsupported and due to the nature of updates on Arch Linux will eventually cause your system to break., and it may be out of date or inaccurate.

This page describes how to automatically update your arch sytem.

First create the necessary configuration and systemd files

/etc/archupgrade.conf
# ignore packages to be updated
# multiple packages seperated by comma
ignore="linux,pacman"
log="/var/log/archupgrade.log"
mailto="<your mail address>"
/etc/sytemd/system/archupgrade.timer
[Unit]
Description=Upgrade arch

[Timer]
OnCalendar=Wed 02:07:00
Persistent=true
Unit=archupgrade.service

[Install]
WantedBy=default.target
/etc/systemd/system/archupgrade.service
[Unit]
Description=Upgrade arch with pacman

[Service]
Type=oneshot
User=root
Environment=SHELL=/bin/sh
Environment=PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
ExecStart=/usr/local/bin/archupgrade minimal reboot

Once your have created the files above add the script to run the update in /usr/local/bin/archupgrade and make it executable

#!/bin/bash

function log() {
	date=$(date +'%Y-%m-%d %H:%M')
   echo "[$date] $1" >> $log 2>&1
}

function mail() {
	host=$(hostname)
	day=$(date +'%Y-%m-%d')
	msg=$(grep "$day" $log)
	echo -e "$msg" | mailx -s "Arch upgrade: $host" $mailto
}


function list_updates() {
	list=$(pacman -Qu)
	if [ $? -ne 0 ]; then
		echo "nothing to do"
		exit 0
	else
		echo "$list"
	fi
}

function will_reboot() {
  dd=$(date +'%Y-%m-%d')
 grep $dd $log | grep "upgrading linux..."
 if [ $? -eq 0 ]; then
   log "Will reboot in 10 minutes"
		shutdown -r +10
	else
		log "no linux kernel upgrade"
	fi
}

function minimal() {
	pacman -Syu --ignore=$ignore --noconfirm --overwrite '*' | tee -a $log
	if [ $? -ne 0 ]; then
		log "Upgrade not complete"
		mail "Minimal upgrade not complete"
		exit 1
	fi

	log "minimal upgrade done"

  if [ $reboot -eq 1 ]; then
		will_reboot
	fi
	mail "minimal upgrade done"
}

function full() {
 # do an archlinux-keyring before upgrading
	sudo pacman -Sy --noconfirm archlinux-keyring  | ts '[%Y-%m-%d %H:%M:%S]' | tee -a $log
	sudo pacman -Syu --noconfirm --overwrite '*' | ts '[%Y-%m-%d %H:%M:%S]' | tee -a $log
	if [ $? -ne 0 ]; then
		log "Upgrade not complete"
		mail "Full upgrade not complete"
		exit 1
	fi

  if [ $reboot -eq 1 ]; then
		will_reboot
	fi
	mail "full upgrade done"
}

config="/etc/archupgrade.conf"
if [ ! -f $config ]; then
	echo "No config file found: $config"
	exit 1
fi
 source $config
pacman -Sy

reboot=$2
if [ -z $reboot ]; then
	reboot=0
else
 reboot=1
fi

case "$1" in
	list_updates)
		list_updates
		;;
	minimal)
		minimal $2
		;;
	full)
		full $2
		;;
	*)
		echo "$0 <list_updates|minimal|full> [reboot]"
		echo "reboot: will only reboot if a kernel update has been done"
		exit 0
		;;
esac
chmod a+x /usr/local/bin/archupgrade

Now enable the timer

systemctl enable --now archupgrade.timer