Are Linux Updates Available? Status Information from Arch or Debian/Ubuntu for your status bar (e.g. i3blocks/i3status for i3 wm)

When you run Linux on your work machine (desktop, laptop, etc), you usually want to be informed in some way about system updates being available. It might be that the package manager software takes care of this – sometimes in annoying ways, like by opening up a notification window once updates are available. However, there Linux + UI variants in which you are not automatically notified about updates being available. One example would be Arch Linux with i3 (or i3-gaps) as window manager. Pacman, the package manager of Arch Linux, does by concept not pop-up any notification windows – for a bunch of good reasons (one of them is that there needs not be any UI at all on an Arch system). However, if you use an UI, like i3/i3-gaps, then there most likely is a status area/status bar (in case of i3: i3status, i3blocks), which would be the perfect area for showing status information about available updates in an unobtrusive way.A script that checks for status of updates would best output the status as a short, concise, and human understandable string. This allows for the output to be included in all types of status bars, independently of the software in use. Below there are two such scripts for Debian/Ubuntu and Arch Linux. Both check for if updates are available and if a reboot of the machine is required.Helpful to keep in mind: if you want to adapt those scripts to your own needs, for debug purposes you can simply call them in your terminal. If they work as you expect they will print the desired output into your terminal; and during debugging you will get any error messages there too.Ubuntu LogoThe script for Debian/Ubuntu (same as the one in this post:

##!/bin/bash

# count how many updates we have got
ups=`/usr/lib/update-notifier/apt-check --human-readable | head -1 | awk '{print $1;}'`

# print the results
if [ "$ups" -eq "1" ]
then
  echo "There is 1 update"
elif [ "$ups" -gt "1" ]
then
  echo "There are $ups updates"
elif [ -f /var/run/reboot-required ]; then
     echo 'Reboot required'
else
  echo "Up to date"
fi

https://en.wikipedia.org/wiki/Arch_Linux

The script for Arch is a little longer and adapted from here (I’ve added some sloppy functionality to check if a reboot is required, which incorporates the 2nd small script below):

#!/usr/bin/env python3
#
# Copyright (C) 2017 Marcel Patzwahl
# Licensed under the terms of the GNU GPL v3 only.
#
# i3blocks blocklet script to see the available updates of pacman and the AUR
import subprocess
from subprocess import check_output
import argparse
import re


def create_argparse():
    parser = argparse.ArgumentParser(description='Check for pacman updates')
    parser.add_argument(
        '-b',
        '--base_color',
        default='green',
        help='base color of the output(default=green)'
    )
    parser.add_argument(
        '-u',
        '--updates_available_color',
        default='yellow',
        help='color of the output, when updates are available(default=yellow)'
    )
    parser.add_argument(
        '-a',
        '--aur',
        action='store_true',
        help='Include AUR packages. Attn: Yaourt must be installed'
    )
    parser.add_argument(
        '-q',
        '--quiet',
        action = 'store_true',
        help = 'Do not produce output when system is up to date'
    )
    parser.add_argument(
        '-w',
        '--watch',
        nargs='*',
        default=[],
        help='Explicitly watch for specified packages. '
        'Listed elements are treated as regular expressions for matching.'
    )
    return parser.parse_args()


def get_updates():
    output = check_output(['checkupdates']).decode('utf-8')
    if not output:
        return []

    updates = [line.split(' ')[0]
               for line in output.split('\n')
               if line]

    return updates

def get_aur_updates():
    output = ''
    try:
        output = check_output(['yaourt', '-Qua']).decode('utf-8')
    except subprocess.CalledProcessError as exc:
        # yaourt exits with 1 and no output if no updates are available.
        # we ignore this case and go on
        if not (exc.returncode == 1 and not exc.output):
            raise exc
    if not output:
        return []

    aur_updates = [line.split(' ')[0]
                   for line in output.split('\n')
                   if line.startswith('aur/')]

    return aur_updates


def matching_updates(updates, watch_list):
    matches = set()
    for u in updates:
        for w in watch_list:
            if re.match(w, u):
                matches.add(u)

    return matches


def reboot_required():
    output = check_output(['arch-reboot-required.sh']).decode('utf-8')
    if 'yes' in output:
        return ', reboot required'

    return ""



message = "<span color='{0}'>{1}</span>"
args = create_argparse()

updates = get_updates()
if args.aur:
    updates += get_aur_updates()

update_count = len(updates)
if update_count > 0:
    info = str(update_count) + ' updates available'
    matches = matching_updates(updates, args.watch)
    if matches:
        info += ' [{0}]'.format(', '.join(matches))
    print(message.format(args.updates_available_color, info + reboot_required()))
elif not args.quiet:
    print(message.format(args.base_color, ' up to date' + reboot_required()))

The small 2nd script to check if a reboot is required with Arch Linux (adapted from here). It currently only checks if the Linux kernel in use has the same version number as the newest installed Linux kernel package on the system. In case those disagree, the assumption is that an update did ship a new kernel package, which requires a reboot to come into use.

#!/usr/bin/bash

s1=$(pacman -Q linux | sed 's/linux //; s/-/./g')
s2=$(uname -r | sed 's/-ARCH//; s/-/./g')

#echo \"$s1\"
#echo \"$s2\"
if [ "$s1" == "$s2" ]; then
  echo "no"
else
  echo "yes"
fi

i3_window_manager_logo

To include this status output into your status bar adapting the status bar config file is the way to go. As an example, here is the config that I added to my ~/.config/i3/i3blocks.conf file:

[pacman-updates]
label=ARCH
command=~/path/to/my/arch-update.py -b "#5fff5f" -u "#FFFF85" -w "^linux.*" "^pacman.*"
markup=pango
interval=3600
signal=10

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s