Another magic sysadmin script

Once in a while, Red Hat releases a new kernel package for RHEL.  A few days later, CentOS releases the same kernel package with all the Red Hat copyrighted stuff removed.  Then, once per week, my server pulls in all the updated packages, including the new kernel revisions.  In the past, I’ve just been letting the new kernels pile up for the next reboot, which sometimes takes months to happen.  But I decided that I wanted to use the new kernels right away instead of waiting.  One option was a mandatory weekly reboot.  But that’s unnecessary downtime and extra work for the sysadmin.  So instead I wrote a script that checks for a new kernel version and sends me an e-mail reminding me to schedule a reboot:

#!/bin/bash
#
# Checks to see if the current kernel version matches the newest kernel image in /boot
#

NEWEST_IN_BOOT=`grep vmlinuz /etc/grub.conf | tail -n +2 | head -n 1 | cut -d"-" -f2,3 | cut -d" " -f1`
echo "DEBUG:  Newest available kernel image in /boot is $NEWEST_IN_BOOT"
CURRENT_VERSION=`uname -r`
echo "DEBUG:  Current kernel release is $CURRENT_VERSION"

if [ "$NEWEST_IN_BOOT" != "$CURRENT_VERSION" ]
then
        mail -s "New kernel available" root@dustpuppy <<EOF

******************************
* A new kernel is available. *
* Please schedule a reboot.  *
******************************

Kernel release $NEWEST_IN_BOOT is available in /boot.
Current release is $CURRENT_VERSION.

-root on dustpuppy

EOF
fi

I have this run out of cron on a weekly basis.

Advertisements
  1. #1 by Joshua on May 19, 2010 - 1:07 PM

    I’m sure that I could replace the cut and grep pipeline with a sed one-liner but sed isn’t really my thing. If someone wants to suggest a sed-ism for the above, please do so! Thanks!

    • #2 by Joshua on May 19, 2010 - 1:11 PM

      Awk, too. Though I’m a bit better with awk than sed. At any rate, it was quicker just to do grep | cut | cut but an awk line would be more elegant.

  2. #3 by Andrew D. on May 20, 2010 - 10:48 AM

    Why bother rebooting? That’s what ksplice is for. Maybe there’s a Red Hat port you can use.

    http://en.wikipedia.org/wiki/Ksplice

  3. #4 by Joshua on May 20, 2010 - 11:50 AM

    Ksplice isn’t free for RHEL / CentOS / Ubuntu Server. Only Ubuntu desktop has a FOSS implementation.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: