this post was submitted on 12 May 2025
9 points (90.9% liked)

Arch Linux

8616 readers
19 users here now

The beloved lightweight distro

founded 5 years ago
MODERATORS
 

Hello everyone! I want to write a short script to let me know if there has been more than 3 days since the last full system update whenever I open a terminal (run from .zshrc). Ive got something cobbled together, but sadly it only checks for the last full system update from pacman directly because of the way it looks at the pacman logs.

My question is, how can I make it so that if EITHER pacman directly or yay runs a full system update, it will update something like a persisitent environment variable with a UNIX timestamp?

I've also considered writing a hook to run after pacman fully updates the system since yay runs pacman under the hood anyway, but I can't figure out how to make the hook recognise if it was a successful full system update.

Here is what I have so far:

#!/bin/zsh

last_upgrade_date=$(grep -m 1 'full system upgrade' /var/log/pacman.log | cut -d ' ' -f 1 | tr -d '[]')
last_upgrade_sec=$(date --date="$last_upgrade_date" +%s) # Convert to UNIX timestamp

last_upgrade_sec=$(date -d "$last_upgrade_date" +%s)
now_sec=$(date +%s)

days_since=$(( (now_sec - last_upgrade_sec) / 86400 ))

if (( days_since > 3 )); then
	echo "Days since last update: $days_since day(s)" >> /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 "Kernel mismatch detected. Reboot recommended." >> /dev/tty
fi

Thanks in advance!

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 1 week ago* (last edited 1 week ago) (1 children)

Apart from trying the hook way, I would default to just checking the timestamp of /var/lib/pacman/sync/core.db and extra.
As any upgrade should be a system upgrade.

[–] promitheas 2 points 1 week ago* (last edited 1 week ago) (1 children)

This seems like an easy implementation, and I can skip the variable and just directly check the timestamp from the script i guess. My question is, if I install something, say yay -S a_single_package does that also update the timestamp of the core.db file?

[–] [email protected] 1 points 1 week ago* (last edited 1 week ago) (1 children)

-S should not even try to refresh the database, that is what -Sy is for. And doing any variation of -Sy without also u (upgrade) is the unsupported partial "upgrade", so it is possible that the time changes but only in the case of misuse.

Also noticed you can just check the mtime of the directory itself, /var/lib/pacman/sync - directory mtime does not change when the files change content but pacman/alpm probably downloads the new databases to some temp files then moves them into the directory, changing it's modify time (see stat, stat -c '%Y').

[–] [email protected] 1 points 1 week ago

From what I remember, you can run -Sy, so long as the next pacman -S <package>... has the -u flag set.