Michael Trojanek (relativkreativ) — Bootstrapper and creator of things

This article was published on May 14th 2014 and takes about 2 minutes to read.

Use it with caution — it is probably still valid, but it has not been updated for over a year.

Simple update notifications for your CentOS/RedHat-server

Running a production system requires to keep it up to date. On the other hand you do not want to blindly install all available updates, but review which packages can and should be updated. So you need to be notified when updates are available.

For yum-based systems there is a package called yum-cron which can check for updates in the background and (among other things) send you an email with a list of available updates. However, installing it means two more packages on your system, another configuration file to take care of and (more importantly) an additional service running in the background. I hardly find this necessary for such a simple task.

Modify the MOTD

/etc/motd is a file which is displayed to every user after logon. Since I log in to my server quiet often, it's the perfect way to display a small notification when updates are available. Take the following bash-script:

#! /bin/sh

UPDATES_COUNT=$(yum check-update --quiet | grep -v "^$" | wc -l)

if [[ $UPDATES_COUNT -gt 0 ]]; then
  echo "Updates available: ${UPDATES_COUNT}" > /etc/motd
  > /etc/motd

I created it as root and saved it as /root/bin/update-motd but since updates are only checked (and not installed) any user can run it. However, hardly any harm can be inferred from it and since it's not tied to a specific user, I keep it that way.

All that's left to do is making this script run periodically. Make it executable (chmod +x /root/bin/update-motd), edit the crontab (crontab -e) and add the line 00 00 * * * /root/bin/update-motd. This makes the script run every midnight.

One caveat: This script simply overwrites your MOTD. If you have some fancy MOTD displayed you have to make sure that the updates count is prepended to it (maybe by saving a template and concatenating it in the script).

Notifications via email

If you do not login to your server on a regular basis, it makes sense to be notified of available updates via email. A modified version of the script above:

#! /bin/sh

UPDATES=$(yum check-update --quiet | grep -v "^$")
UPDATES_COUNT=$(echo "$UPDATES" | wc -l)

if [[ $UPDATES_COUNT -gt 0 ]]; then
  echo "$UPDATES" | mail -s "Updates for $(hostname): ${UPDATES_COUNT}" you@yourdomain.com

Of course this only works if the mail-command is available on your system. If it is not, instead of installing sendmail and running a SMTP-server just for this one daily email, consider the ssmtp-package. It can connect to an SMTP-server anywhere on the net and send an email on your behalf (providing that you have a mail account there).

Expand your DevOps skills!

Join hundreds of Rails developers and operators on my email list and get my ebook Build Your Own Rails Server as a free welcome gift.

No spam — guaranteed. You can leave at any time.