Count Upgradable Packages on Alpine Linux
I like to use Alpine Linux for cloud servers because it’s extremely small, so I don’t have to add extra RAM just to run the operating system. But I like knowing when updated packages are available.
You need root to update the local list of available packages first, but that can be automated safely (see below):
doas apk update
Just asking Alpine is easy, and doesn’t require extra permissions:
apk list -u
That gives you a detailed list of upgradable packages. Or to get a quieter result with just the names:
apk list -uq
And this being Linux, it’s easy to get a word count with wc (yes, it’ll count hyphenated package names as a single word):
apk list -uq | wc -w
Automating the List Updates
To get APK to update its package list daily, I’ve put the following in /etc/periodic/daily/apk-update.sh:
#!/bin/sh
apk update
That doesn’t install anything, so it’s safe to let it check on a daily basis. Of course that doesn’t tell me anything, it just makes sure the list is relatively current when I ask it.
Friendly Reports on Login
For the actual report, I’ve written a short shell script (kept simple so it’ll run in busybox) called count-apk.sh to give me the count and names, but only if there aren’t too many of them:
#!/bin/sh
UPGRADABLE=`apk list -qu`
COUNT=`echo "$UPGRADABLE" | wc -w`
if [ $COUNT -gt 9 ]; then
echo $COUNT packages available to upgrade.
elif [ $COUNT -gt 1 ]; then
echo $COUNT packages available to upgrade:
printf '%s\n' "$UPGRADABLE"
elif [ $COUNT -gt 0 ]; then
echo 1 package available to upgrade:
printf '%s\n' "$UPGRADABLE"
else
echo No packages available to upgrade.
fi
And to get that reported every time I log in, I’ve put it in my ~/.profile.
~/bin/count-apk.sh
Meanwhile I’ve been trying to decide how much I want to automate maintenance on these systems. I don’t want to auto-update everything and risk breaking changes, but I don’t want to have to log into every internet-reachable virtual server on a weekly basis to keep it current either.