ArchLabs Linux

Polybar pkg module (updates.sh) not updating

Hi, I have to first say, I love this distro! My first archlinux distro, and I dont think I would ever switch to anything else! I have used multiple DEs as well as WM, and I decided to stay with openbox, its truly amazing with polybar and plank, etc, lots of customization’s, which I love! I am a beginner/intermediate linux user, I am trying to learn more, so any help and guidance would be greatly appreciated!

The issue I am having is that the updates module in polybar will not update if it has less than 1 update available. For example, it says I have this many number of updates, I perform the updates, but the reading on the bar is stuck at 1…I am copying information here, to see if anyone can help me:

This is from the polybar modules.conf file:

;;;;;;;;;
;; PKG ;;
;;;;;;;;;
[module/pkg]
type = custom/script
exec = updates.sh
exec-if = "ping -q -w 2 -c 1 176.34.135.167 > /dev/null"
label-padding = 1
label = %output%
tail = true

This is from my updates.sh file:

#!/usr/bin/env bash

BAR_ICON=""
NOTIFIED=0
NOTIFY_ICON=/usr/share/icons/gnome/32x32/apps/system-software-update.png

get_total_updates()
{
    UPDATES=$(checkupdates 2>/dev/null | wc -l)
}

while true; do
    # print the icon first to avoid gibberish in polybar
    echo $BAR_ICON

    get_total_updates

    # notify user of updates
    if (( NOTIFIED == 0 )) && hash notify-send >/dev/null 2>&1; then
        if (( UPDATES > 50 )); then
            notify-send -u critical -i $NOTIFY_ICON "Updates Available" "$UPDATES packages"
        elif (( UPDATES > 25 )); then
            notify-send -u normal -i $NOTIFY_ICON "Updates Available"  "$UPDATES packages"
        elif (( UPDATES > 2 )); then
            notify-send -u low -i $NOTIFY_ICON "Updates Available" "$UPDATES packages"
        fi
        NOTIFIED=1
    fi

    # when there are updates available
    # every 10 seconds another check for updates is done
    while (( UPDATES > 0 )); do
        (( UPDATES == 1 )) && echo "$UPDATES Update" || { (( UPDATES > 1 )) && echo "$UPDATES Updates"; }
        sleep 10
        get_total_updates
    done

    # when no updates are available, use a longer loop, this saves on CPU
    # and network uptime, only checking once every 30 min for new updates
    while (( UPDATES == 0 )); do
        sleep 1800
        get_total_updates
    done
done

I am thinking that within the updates.sh file near the bottom there where is says while … I need to modify something there but im not sure what that would be…

Any help/guidance would be greatly appreciated! Also as a side note, does anyone have any book or something I could read in my spare time to do shell scripting so that I could have solved this myself? I am a firm believer of give a man a fish, he eats for a day, teach me to fish, so I can ‘eat’ my entire life! hahah

Thanks! :slight_smile:

That script is old and will be put out of service soon in favour of a better version.

First change the polybar module to be as follows

[module/pkg]
type = custom/script
exec = tail -F /tmp/.update_status
exec-if = [ -p /tmp/.update_status ]
label-padding = 1
tail = true

Now edit/create ~/bin/check-update, you’ll need to have both pacman-contrib and inotify-tools packages installed

#!/bin/bash

# somewhat deamon update count + icon
# written by Nathaniel Maia, 2019

ICON=""

if ! hash checkupdates inotifywait >/dev/null 2>&1; then
	echo 'error: check-update: requires "pacman-contrib" and "inotify-tools" installed' >&2
	exit 1
fi

[ -p /tmp/.update_status ] || mkfifo /tmp/.update_status

# wait until were connected
until ping -c 1 'archlinux.org' >/dev/null 2>&1 || ping -c 1 'archlabslinux.com' >/dev/null 2>&1; do
	sleep 10
done

# loop and dump info to the pipe
while true; do
	if (( ( COUNT = $(checkupdates 2>/dev/null | wc -l) ) > 0 )); then
		echo "$COUNT $ICON" >/tmp/.update_status
		# recheck when the log is written to or after 1.5 hours
		inotifywait -q -q -t 5000 -e close_write /var/log/pacman.log
		sleep 3
	else
		echo "" >/tmp/.update_status
		sleep 5000 # only do a recheck every 1.5 hours to save some cpu time
	fi
done

To tie it all together all you need to do it start the script in the background before polybar (al-polybar-session), likely in ~/.config/openbox/autostart

sed -i '/al-polybar-session/ i check-update &' ~/.config/openbox/autostart

That’ll insert check-update & above any line containing al-polybar-session in the openbox autostart file.


A good starting point is the shell grammar section of

man bash

These are some other good resources I’ve used a ton over the last few years

4 Likes

OMG, first off, I have to say thank you so much for replying! Second, I really appreciate the reading material you have provided! You can’t begin to understand how hard it is to find ‘good/valuable’ material on the subject; mostly everything is either too basic or too involved, I always find there is nothing gradual or like middle ground to this stuff; ugh I digress!

Now on to what you had offered…so I was able to successfully use your script, thank you for providing that. If you wouldn’t mind, I have some positive critic and my opinions…

I like the original script better, for a few reasons. First, yours doesn’t look as nice IMHO, its super functional but I just didn’t like that it wouldn’t show me the icon if there were no updates or until it actually ran…which i’m not certain but its like every 1.5 hours or so…I also really liked the color scheming of the original as well, but again that is very subjective. So I reverted back to the original, both the module and actual script itself…

On an even better note, I was able to modify the original script to only display the icon when there are no updates…I obviously used your script to like decipher as to what was actually going on since it was much easier to read… I looked at the do at the bottom, and literally just added after this piece, " while (( UPDATES == 0 )); do" in the next line, “echo $BAR_ICON”

It’s perfect now. Thank you so much again, you are a true resource! I hope to be as good as you someday with Archlinux/Archlabs (linux as a whole) and scripting! I have tried so many distros, and this one is my absolute favorite! I will most definitely become a patreon to show my support to the community!! :smiley: If i’m ever in Canada, I will definitely give you a shout, we can link…

1 Like

Hey thanks for the kind words, always glad to help.

I feel like most is a matter of personal taste, lately I’ve been on the “less is more” train of thought but I know what you mean by the previous being more understandable (they’re both mine, they were just written at different times). On the point of showing an icon when no updates are available you’ve shown yourself capable of tweaking the other, same can be done here. I’ll add an extra comment so it makes more sense

...
else
	# there were 0 updates available so clear the pipe
	echo "" >/tmp/.update_status
	sleep 5000 # only do a recheck every 1.5 hours to save some cpu time
fi

On the note of why exactly the other is better, regardless of style or readability the first will not update the bar when you update your system so you can have the bar showing updates available after just updating.

Either way it’s your system use what you like best,

Cheers

I know all too well believe me! :slight_smile:

I found just picking up others scripts and editing them to be most helpful when I was starting out as some of the hardest parts were done and I could tweak and learn as I go and break things. Also inspiration, seeing something useful for one purpose sometimes gives you an idea for something similar but different.