[HowTo] Check if updates may require system restart

Difficulty: ★★☆☆☆

Description

Some system updates will require you to restart your system. Packages includes microcode, kernel, driver, xorg and systemd as likely candidates. Below is a list of packages which may warrent a system restart. If you can think of other packages to include in the check just add the package(s) to the $reboot regex in the script.

  • ucode
  • cryptsetup
  • linux
  • nvidia
  • mesa
  • systemd
  • wayland
  • xf86-video
  • xorg

Utility script

The script calls the checkupdates script - and yay if installed - and uses regular expression to suggest a possible system restart.

#!/usr/bin/env bash
reboot="(ucode|cryptsetup|linux|nvidia|mesa|systemd|wayland|xf86-video|xorg)"
if [[ $(which yay) =~ (yay) ]]; then
    updates=$(checkupdates; yay -Qua)
else
    updates=$(checkupdates)
fi
echo "$updates"
if [[ $updates =~ $reboot ]]; then
    echo "Updating possibly requires system restart ..."
fi

Usage

As noted in comments the check is very broad and not intended by any means to be authoritative - it only serves the purpose of suggesting - before updates are installed - if system restart could be necessary.

Credit and inspiration

Inspiration from @Kresimir and EndeavourOS forum [1] [2] and the resulting script [3]

12 Likes

That only works if there are pending updates:

xorg-server 1.20.8-4 -> 1.20.9-1
xorg-server-common 1.20.8-4 -> 1.20.9-1
xorg-server-xwayland 1.20.8-4 -> 1.20.9-1
Updating possibly requires system restart ...

After I updated those packages, the script prints nothing.

However, needrestart (available in the AUR) tells me:

Scanning processes...                                                                    
Scanning candidates...                                                                   
Scanning processor microcode...                                                          
Scanning linux images...                                                                 

Running kernel seems to be up-to-date.

The processor microcode seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

User sessions running outdated binaries:
 gdm @ session #1: gdm-x-session[986]
 yochanan @ session #3: gdm-x-session[1349]
5 Likes

You are correct it only gives you a preliminary guess by checking for some common packages in the current update set.

Interesting - I didnt’ know of that package - the script is not intended as the ultimate authority - just a hint

This should be a pretty good update hook :thinking:

2 Likes

yes hook as

#/etc/pacman.d/hooks/reboot.hook
[Trigger]
Operation = Upgrade
Operation = Install
Type = Package
Target = *

[Action]
Description = reboot needed ?
When = PostTransaction
Exec = /etc/pacman.d/hooks.bin/reboot.sh "-ucode|cryptsetup|linux-|nvidia|mesa|systemd|wayland|xf86-video|xorg-"
NeedsTargets
#!/bin/bash
#/etc/pacman.d/hooks.bin/reboot.sh

pkgsboot="${1}"
declare -a packages=($(cat -))   # all packages in pacman transaction

for package in "${packages[@]}"; do
 if [[  "$package" =~ $pkgsboot  ]]; then
    echo "Updating possibly requires system restart ($package)"
    #exit 0 # can comment this line or not ...
 fi
done

note: hooks are sorted by name, so prefer a name like zreboot.hook

script with “linux” is not exact we a lot of “**linux**:roll_eyes:
and also here linux-** is too big as xorg …

5 Likes

Nice. Maybe a good idea for a new pamac feature.

Like: If one or more of those packages are going to be upgraded in pamac: Display a small warning dialog: Please reboot after upgrade…

edit
Filed a feature request: [Feature Request] Show warning dialog when packages are installed which require a reboot (#881) · Issues · Applications / pamac · GitLab :wink:

3 Likes

here, the selection is much too vague :wink:

for: linux-* , xorg-*
we would need a list of complicated regex, and pamac runs on arm and archlinux .

ps: exists also a “simple” pacman hook but much less flexible


I have also some script for find errors and .pacnew after update (read pacman.log)

# find last warnings chmod in log
# warning.*permissions differ

# find permissions differ in log
# usage pacnewsListLog "2018-06"
permissionsListLog()
{
    declare -a line
    declare -A results
    declare d=${1:-$(date +%Y-%m)}      # monthly logs, day: -%d
    declare d=${1:-$(date +%Y-%m-%d)}   # current day
    
    while read -ra line ; do
        if [ -d "${line[-1]}" ]; then
            results["${line[-1]}"]=1 # uniqu
        fi
    done < <(grep -E "${d}.*warning.*permissions differ" /var/log/pacman.log)    
    if [[ "${#results[@]}" > 0 ]] ; then
        echo ""
        for dir in "${!results[@]}"; do
            #echo -en "\t${dir} "
            echo -e "\t$(pacman -Qo "${dir}")"
        done
        #printf  "\t%s\\n" "${!results[@]}"
        echo -e "${c_re}::${c_r} Your system configuration is no longer up to date ?"
        echo -e "${c_re}::${c_r} change by chmod permissions"
    fi
}
permissionsListLog

# find .pacnew in log
# usage pacnewsListLog "2018-06"
pacnewsListLog()
{
    declare -a line
    declare -A results
    declare d=${1:-$(date +%Y-%m)}      # by default monthly files, day: -%d
    while read -ra line ; do
        #echo "${line[-1]}"
        if [ -f "${line[-1]}" ]; then
            results["${line[-1]}"]=1 # uniqu
        fi
    done < <(grep -E "${d}.*warning.*\.pacnew$" /var/log/pacman.log)    
    if [[ "${#results[@]}" > 0 ]] ; then
        echo ""
        printf  "\t%s\\n" "${!results[@]}"
        echo -e "${c_re}::${c_r} Your system configuration is no longer up to date"
    fi
}
# find last pacsave in log
pacnewsListLog
3 Likes

Ok, I see you point.

Just reading about those hooks. They were new to me.
Is pamac also triggering those pacman hooks?

no hook with pamac

2 Likes

Actually it was a couple of topics on endeavouros forum and input from @Kresimir which sparked this little script.

1 Like

+1 for needrestart.

Use a simple alias to do this:

alias upd='sudo pacman -Syu && sudo needrestart'

I do it different; I check mirror status before pacman as well.