Hello everyone :)

I have this in a script which I call from my .zshrc:

#!/bin/zsh

# ~/.bin/check_last_update.sh

GREEN='\033[92m'
YELLOW='\033[93m'
RED='\033[91m'
RESET='\033[0m'

# Check if there are available updates for packages
num_packages_to_update=$(checkupdates | wc -l)

if (( num_packages_to_update >= 0 && num_packages_to_update <= 20 )); then
	echo -e "${GREEN}Available updates: $num_packages_to_update${RESET}" >> /dev/tty
elif (( num_packages_to_update > 20 && num_packages_to_update <= 50 )); then
	echo -e "${YELLOW}Available updates: $num_packages_to_update${RESET}" >> /dev/tty
else
	echo -e "${RED}Available updates: $num_packages_to_update${RESET}" >> /dev/tty
fi

# Kernel version check
running_kernel=$(uname -r)
installed_kernel=$(pacman -Q linux | awk '{print $2}' | cut -d '-' -f1 | cut -d '.' -f1,2,3)

running_kernel_base=$(echo "$running_kernel" | cut -d '-' -f1)

if [[ "$running_kernel_base" != "$installed_kernel" ]];
then
	echo -e "${RED}Kernel mismatch detected. Reboot recommended.${RESET}" >> /dev/tty
fi

It works, but when I open a new terminal it may take up to 2 seconds for the prompt to show up. Is there a way of speeding this up?

Would it be a good idea to perform the checks in the background every hour or so, maybe with a systemd timer or something, and cache the results (or possibly to make it even more efficient, just store a file somewhere out of the way containing the number of updates and if there is a kernel mismatch - maybe on separate lines so when I open a terminal I can easily query the file and get exactly what I need) so that when I open a new terminal instead of checking with the live repositories it checks the cached data? Also, I guess this would mean that if a package has become available for update after the last time the check script runs, then the number it tells me will be less than the actual number of available updates, right?

Thanks in advance!

  • tetris11@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    18 days ago

    Yeah, run checkupdates in a timer, and then in your script invoke checkupdates -n

    https://man.archlinux.org/man/checkupdates.8

    -n, --nosync
    Do not sync the temporary database, works   
    best when the temporary database is   
    updated regularly through some other   
    means such as a cronjob or systemd timer.
    
    • promitheas@programming.devOP
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      17 days ago

      This seems to be basically what I want to achieve, except I realised it only works on official arch repos and so doesnt sync the DB for the AUR, which I’d like. I started looking into it and a way I can use just yay to sync the local DB with the remote since its just a wrapper for pacman, but I got a bit lost.

      Using yay to sync both official and aur I think also means I need to create only one .timer and one .service files to sync the DBs, instead of having one of each for checkupdates and whatever yay command I use, right?

      Any ideas for what I could use without needing to enter sudo password since it needs to run in the background?

      Thanks

      EDIT: I solved the passwordless issue I think by adding this to my sudoers:

      myuser ALL=(ALL) NOPASSWD: /usr/bin/yay -Sy --noconfirm
      

      I verified by running yay -Sy --noconfirm and it worked without asking for password so I think if I use that in my systemd timer service if will work, but now I would like to check against the local DB everytime I open the terminal.

      EDIT 2: In my .bin/check_last_update.sh script I replaced this line

      num_packages_to_update=$(checkupdates | wc -l)
      

      with this:

      num_packages_to_update=$(yay -Qu | wc -l)
      

      I think it works now, but I’m not sure if the -Qu flags also check for AUR updates.