Check if a Red Hat / CentOS server needs a reboot after updates

At a client site updates were installed on Red Hat systems, but they were not always rebooted after a kernel update. So I needed a quick way to test if a server had a newer kernel installed than it was currently running. Just to save it for future use :

#!/bin/bash

LAST_KERNEL=`/bin/rpm -q --last kernel | /usr/bin/head -1 | /usr/bin/perl -pe 's/^kernel-(\S+).*/$1/'`
RUNNING_KERNEL=`uname -r`

if [[ $LAST_KERNEL != $RUNNING_KERNEL ]];
then
  echo "reboot pending. installed $LAST_KERNEL, currently running $RUNNING_KERNEL"
else
  echo "system OK"
fi

#rm $0
exit 0

(Debian and Ubuntu based systems have a somewhat simpler to use reboot pending flag on the filesystem)