You are not logged in.

#1 2022-09-07 14:17:51

dogknowsnx
Member
Registered: 2021-04-12
Posts: 365

[SCRIPT] Desktop notifications for Arch Linux package updates

Maybe you're new to Arch Linux, or you simply like - or possibly miss - the warm feeling of something (greenish) popping up whenever updates have arrived, this might be for you. There are probably countless ways of achieving the same thing out there already. Well - here's another one...

For more details about my small contribution please see the prologue in the script below.

#!/bin/sh
#
#
### Description:
#
# This script - let's call it 'updatenotif.sh' - 
# is to be used on Arch Linux and notifies about
# updates based on the number of updates available.
#
# It has been tested only on 'wlroots'-based
# wayland compositors. If you're running Xorg,
# make sure to have a working notification daemon.
# You can test it by issuing e.g.:
# 'notify-send Breathe!'
#
# Personally I'm using a crontab to run the script hourly
# - if you prefer a systemd timer, please refer to:
#	"https://wiki.archlinux.org/title/Systemd/Timers"
#
#
### Requirements/Dependencies:
#
# 'libnotify',
# 'pacman-contrib',
# A notification daemon, e.g. 'mako' or 'fnott' (aur),
# please also see: "https://wiki.archlinux.org/title/Desktop_notifications",
# 'papirus-icon-theme' (optional),
# 'cronie' (optional) # You can set up a systemd timer instead
#
# 'libnotify' needs to know which bus address to use,
# so we have to add the environment variable to our crontab.
# To get your bus address, issue 'echo $DBUS_SESSION_BUS_ADDRESS'.
# My crontab looks like this:
#
#	DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
#	@hourly $HOME/.local/bin/updatenotif.sh >/dev/null 2>&1
#
# Edit your crontab accordingly by issuing 'crontab -e',
# for more information: "https://wiki.archlinux.org/title/Cron".


# Variables
UPD=/tmp/updates

# Uncomment the following command in order to
# check for a network connection first
#ping -c 1 www.archlinux.org >/dev/null 2>&1 || exit 2

# Create a temporary file (if it doesn't already exist)
# for storing update counts, starting with zero
[ -f $UPD ] || echo 0 > $UPD

# Check for available updates and count them
checkupdates | wc -l >> $UPD

# Check whether new updates have arrived
# since the last notification
if
	[ $(tail -n 1 $UPD) -le $(tail -n 2 $UPD | head -n 1) ];
then # Do nothing if there are no new updates
	exit 0
else # Notify about the number of available updates
	notify-send -t 10000 \
		--icon=/usr/share/icons/Papirus-Dark/48x48/apps/mx-updater.svg \
		"Updates:" "$(tail -n 1 $UPD)"
fi

Reason for editing: '/home/myusername' -> '$HOME'

Last edited by dogknowsnx (2022-09-08 12:33:27)


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#2 2022-09-07 15:27:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,279
Website

Re: [SCRIPT] Desktop notifications for Arch Linux package updates

You shouldn't need to store an endless string of numbers in the temp file, just one would suffice (which simplifies all the head|tail work):

#!/bin/sh

UPD=/tmp/updates

cur=$(checkupdates | wc -l)
read prev 2>/dev/null <$UPD

[ $cur -le ${prev:-0} ] && exit
echo $cur >| $UPD

notify-send -t 10000 \
	--icon=/usr/share/icons/Papirus-Dark/48x48/apps/mx-updater.svg \
	"Updates:" "$cur"

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2022-09-07 15:42:07

dogknowsnx
Member
Registered: 2021-04-12
Posts: 365

Re: [SCRIPT] Desktop notifications for Arch Linux package updates

@Trilby Cool - thanks.

EDIT: I will leave the original script as is, since I've gotten used to checking '/tmp/updates' for the number of packages I've updated at a time...
EDIT#2: I also deliberately avoided issuing 'checkupdates' more than once.

Last edited by dogknowsnx (2022-09-07 16:32:02)


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

Board footer

Powered by FluxBB