Check For Available Updates with YUM

If you’re using CentOS, Fedora or Red Hat Linux, you are probably familiar with the yum package manager. One of the really useful options for yum is checking whether there are any available updates to be installed.

Check For Updates with YUM

If you use check-update parameter with yum, it will show you the list of any available updates:

root@centos:~ # yum check-update
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: rep-centos-fr.upress.io
* epel: mirror.in2p3.fr
* extras: rep-centos-fr.upress.io
* updates: ftp.pasteur.fr

ansible.noarch 2.7.8-1.el7 epel
datadog-agent.x86_64 1:6.10.1-1 datadog
libgudev1.x86_64 219-62.el7_6.5 updates
nginx.x86_64 1:1.15.9-1.el7_4.ngx nginx
oci-systemd-hook.x86_64 1:0.1.18-3.git8787307.el7_6 extras
polkit.x86_64 0.112-18.el7_6.1 updates
systemd.x86_64 219-62.el7_6.5 updates
systemd-libs.i686 219-62.el7_6.5 updates
systemd-libs.x86_64 219-62.el7_6.5 updates
systemd-python.x86_64 219-62.el7_6.5 updates
systemd-sysv.x86_64 219-62.el7_6.5 updates

Using yum check-update in Shell Scripts

One thing that I didn’t know and am very happy to discover is that yum check-update is actually meant for shell scripting. It returns a specific code after running, you can use the value to decide what do to next.

As usual: return value 0 means everything is fully updated, so no updates are available (and no action is needed). A value of 100 would mean you have updates available.

All we need to do is check the return value variable $? for its value in something like this:

#!/bin/bash

yum check-update

if [ $? == 100 ]; then
    echo "You've got updates available!"
else
    echo "Great stuff! No updates pending..."
fi

Here is how running this script would look if we saved the script as check-yum-updates.sh script:

root@s2:~ # ./check-yum-updates.sh
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: rep-centos-fr.upress.io
* epel: mirror.in2p3.fr
* extras: rep-centos-fr.upress.io
* updates: ftp.pasteur.fr

ansible.noarch 2.7.8-1.el7 epel
datadog-agent.x86_64 1:6.10.1-1 datadog
libgudev1.x86_64 219-62.el7_6.5 updates
nginx.x86_64 1:1.15.9-1.el7_4.ngx nginx
oci-systemd-hook.x86_64 1:0.1.18-3.git8787307.el7_6 extras
polkit.x86_64 0.112-18.el7_6.1 updates
systemd.x86_64 219-62.el7_6.5 updates
systemd-libs.i686 219-62.el7_6.5 updates
systemd-libs.x86_64 219-62.el7_6.5 updates
systemd-python.x86_64 219-62.el7_6.5 updates
systemd-sysv.x86_64 219-62.el7_6.5 updates
You've got updates available!

I’ll revisit this post soon to show you a few more things that can be done with yum check-update functionality.

See Also