Skip to content

Instantly share code, notes, and snippets.

@jfredrickson
Last active November 17, 2022 20:14
Show Gist options
  • Save jfredrickson/b1e8804f756c94222a614571233b6106 to your computer and use it in GitHub Desktop.
Save jfredrickson/b1e8804f756c94222a614571233b6106 to your computer and use it in GitHub Desktop.
Count pacman updates and display in polybar

Count updates

Checks for pacman updates every hour, and echoes the update count to /var/local/countupdates/count.

Polybar can then read this file and display the results.

Prerequisites

  • pacman-contrib

Files

systemd

  • /usr/local/lib/systemd/system/checkupdates.service
  • /usr/local/lib/systemd/system/checkupdates.timer

polybar

  • ~/.config/polybar/scripts/count-updates.sh
  • ~/.config/polybar/config

pacman

  • /etc/pacman.d/hooks/countupdates.hook
[Unit]
Description=Count how many updates are available
[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c '/usr/bin/checkupdates | wc -l > /var/local/countupdates/count'
[Unit]
Description=Count how many updates are available
[Timer]
OnCalendar=hourly
Unit=checkupdates.service
[Install]
WantedBy=timers.target
[module/count-updates]
type = custom/script
exec = ~/.config/polybar/scripts/count-updates.sh
interval = 10
#!/bin/sh
if [ -s /var/local/countupdates/count ] ; then
updates=$(cat /var/local/countupdates/count)
else
updates=0
fi
if [ "$updates" -gt 0 ]; then
echo "# $updates"
else
echo ""
fi
[Trigger]
Operation = Upgrade
Type = Package
Target = *
[Action]
Description = Count the number of packages ready for upgrade.
When = PostTransaction
Exec = sh -c '/usr/bin/checkupdates | wc -l > /var/local/countupdates/count'
@wbollock
Copy link

wbollock commented Feb 2, 2021

Great work, but a little over-engineered.

polybar will run it by itself or throw it on a cron. No need for systemd timers

#!/bin/bash

# provided by pacman-contrib
checkupdates | wc -l

@jfredrickson
Copy link
Author

I agree cron would be a lot simpler than systemd timers. That choice was more of a learning exercise for systemd. As for why I'm storing the update count in a file rather than running checkupdates directly from Polybar:

  • If I'm on a laptop away from wifi, I don't end up with ERROR: Cannot fetch updates in my bar
  • I have a pacman hook that refreshes the count after it's done installing updates, so Polybar can frequently refresh the update count and the update indicator quickly disappears after pacman installs updates

@wbollock
Copy link

wbollock commented Feb 2, 2021

Ha, makes sense! Sorry for necro-ing a two year old gist.

@p0ryae
Copy link

p0ryae commented Nov 14, 2021

You can easily fix the issue for the ERROR: Cannot fetch updates by simply ignoring it using the same one @wbollock suggested but better.

checkupdates 2>/dev/null | wc -l

It's way superior and more convenient then running a whole service and timer for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment